This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| loaded_model = tf.keras.models.load_model('../models/saved_image_classifier/') | |
| loss, acc = loaded_model.evaluate(X_test, y_test, verbose=0) | |
| print('Accuracy: {:5.2f}%'.format(100*acc)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model.save('../models/saved_image_classifier/') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| test_image = read_image("../data/raw/test/82_automobile.png", image_size) | |
| plt.imshow(test_image) | |
| plt.title('test image') | |
| plt.show() | |
| pred_softmax = model.predict(test_image.reshape(1, image_size[0], image_size[1], 3)) | |
| pred_class_index = np.argmax(pred_softmax, axis=1) | |
| target_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] | |
| print("CNN sees", target_names[pred_class_index.item()]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| deer = read_image("../data/raw/train/3_deer.png", (32, 32)) | |
| car = read_image("../data/raw/train/4_automobile.png", (32, 32)) | |
| bird = read_image("../data/raw/train/18_bird.png", (32, 32)) | |
| plt.subplot(131) | |
| plt.imshow(deer) | |
| plt.title('Deer') | |
| plt.subplot(132) | |
| plt.imshow(car) | |
| plt.title('Car') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: tf2-image-classifier-api | |
| channels: | |
| - anaconda | |
| - conda-forge | |
| - defaults | |
| dependencies: | |
| - pip==20.1 | |
| - numpy==1.18.4 | |
| - flask==1.1.2 | |
| - matplotlib==3.2.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def read_image(image_path, image_size): | |
| """ read an image using opencv """ | |
| image = cv2.imread(image_path) #BGR | |
| # convert to RGB | |
| image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| # resize using cubic interpolation, a way to deal with missing data (upsize or downsize) | |
| image = cv2.resize(image, image_size, cv2.INTER_CUBIC) | |
| return image |