def preprocess_image(image, image_size): image = io.imread(image) x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) return x
def read_image(path): try: return preprocess_input( center_crop_and_resize(imread(path)[:, :, :3], image_size=image_size)) except: return None
def __image_classifier(response): """ Generates an EfficientNetB4 model and classifies images. Args: response (dict): Query response to send to API. Should be of itemDataType image. """ # Process images for index, item in enumerate(response['data']): image_base64 = item['itemData'] if image_base64 in cache: print(cache[image_base64]) response['data'][index]['ml_suggest'] = cache[image_base64][0][0][1] else: print(response['data'][index]['filename']) image = __image_decoder(image_base64) image = efn.center_crop_and_resize(image=image, image_size=IMG_SIZE) image = efn.preprocess_input(image) img_array = tf.keras.preprocessing.image.img_to_array(image) img_array = tf.expand_dims(img_array, 0) # Predict prediction = model.predict(img_array) prediction = tf.keras.applications.imagenet_utils.decode_predictions(prediction) # Append prediction to response print(prediction) response['data'][index]['ml_suggest'] = prediction[0][0][1] cache[image_base64] = prediction print(response['data'][index]['ml_suggest']) return response
def infer(model, image_path): image = imread(image_path) image_size = model.input_shape[1] x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) predictions = model.predict(x) predictions_with_labels = decode_predictions(predictions) return {t[1]: t[2] for t in predictions_with_labels[0]}
def vstack_patch(img, coords): patches = [] for c in coords: patch = center_crop_and_resize(img[c[0][1]:c[1][1], c[0][0]:c[1][0]], image_size=224) patch = np.expand_dims(patch, axis=0) temp = np.array(patch, dtype="float") patches.append(temp) return np.vstack(patches)
def predict(image): x = center_crop_and_resize(image, image_size=image_size) image_modified = x x = preprocess_input(x) x = np.expand_dims(x, 0) # make prediction and decode tb._SYMBOLIC_SCOPE.value = True y = model.predict(x) res = decode_predictions(y) # collect result and the modified image data = {'res': res[0][0], 'new_image': image_modified} return data
def predict(img, model): try: image_size = model.input_shape[1] x = center_crop_and_resize(img, image_size=image_size) x = np.expand_dims(x, 0) preds = model.predict(x) prob = np.max(preds) cid = np.unravel_index(preds.argmax(), preds.shape)[1] return cid, prob, x except: return 0, 0, 0
def load_img_imagenet(img_path, args): """Process the image of ImageNet data Args: img_path (string): Path of image Returns: x (array): array of the image """ if args.model == 'vgg16' or args.model == 'densenet201': img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) if args.model == 'vgg16': from keras.applications.vgg16 import preprocess_input elif args.model == 'densenet201': from keras.applications.densenet import preprocess_input x = preprocess_input(x) return x elif args.model == 'efficientnetb0' or args.model == 'efficientnetb7': from efficientnet.keras import center_crop_and_resize, preprocess_input from skimage.io import imread image = imread(img_path) image_size = args.image_size x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) return x elif args.model == 'efficientnet-b7': from efficientnet.keras import center_crop_and_resize, preprocess_input from skimage.io import imread image = imread(img_path) image_size = args.image_size x = center_crop_and_resize(image, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) return x
#name: Image Classification #description: Image classification based of Efficientnet model #language: python #input: file file #output: map classes [Detected classes with probabilities] #tags: demo, panel, files, efficientnet #condition: file.isFile && file.size < 1e6 && (file.name.endsWith("jpg") || file.name.endsWith("jpeg") || file.name.endsWith("png")) #help-url: https://github.com/qubvel/efficientnet import numpy as np from skimage.io import imread from efficientnet.keras import EfficientNetB0 from keras.applications.imagenet_utils import decode_predictions from efficientnet.keras import center_crop_and_resize, preprocess_input image = imread(file) model = EfficientNetB0(weights='imagenet') image_size = model.input_shape[1] _image = center_crop_and_resize(image, image_size=image_size) _image = preprocess_input(_image) _image = np.expand_dims(_image, 0) predicted = model.predict(_image) predicted = decode_predictions(predicted)[0] classes = {} for p in predicted: classes[p[1]] = float(p[2])
| +-- run_efficient_net.py """ frame_folder = os.path.join(os.getcwd(), 'frames') #if only the file is uploaded final_predictions = list() for video_folder in os.listdir('frames'): media_id = video_folder for frame in os.listdir(os.path.join(frame_folder, video_folder)): frame_nr = frame[frame.rfind('_')+1:-4] img_name = os.path.join(frame_folder, video_folder, frame) img = imread(img_name) # preprocess input image_size = model.input_shape[1] x = center_crop_and_resize(img, image_size=image_size) x = preprocess_input(x) x = np.expand_dims(x, 0) y = model.predict(x) prediction = decode_predictions(y, top=nr_classes) pred_as_list = list() for sublist in prediction: for pred in sublist: pred_as_list.append(pred[1]) pred_as_list.append(pred[2]) final_predictions.append([media_id, frame_nr] + pred_as_list) df = pd.DataFrame(final_predictions, columns=df_colnames)