def inception_one_image(image_path): img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = new_inception.predict(x) return predictions
def extract_features_keras(list_images): ## Extract the transfer values of the last Avg_pool layer of new_inception ## model. Weights are kept from new inception model ## A new model is made with Avg_pool as the last layer. Image are processed ## in the CNN and features extracted nb_features = 2048 features = np.empty((len(list_images), nb_features)) model = Model(inputs=new_inception.input, outputs=new_inception.get_layer('avg_pool').output) for i, image_path in enumerate(list_images): img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) predictions = model.predict(x) features[i, :] = np.squeeze(predictions) return features
def preprocess_input(*args, **kwargs): return inception_v3.preprocess_input(*args, **kwargs)
def batch_preprocess_input(x_batch, network): if network == 'Xception': x_batch = xception.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'VGG16': x_batch = vgg16.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'VGG19': x_batch = vgg19.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNet50' or network == 'ResNet101' or network == 'ResNet152': x_batch = resnet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNet50V2' or network == 'ResNet101V2' or network == 'ResNet152V2': x_batch = resnet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'ResNeXt50' or network == 'ResNeXt101': x_batch = resnext.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'InceptionV3': x_batch = inception_v3.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'InceptionResNetV2': x_batch = inception_resnet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'MobileNet': x_batch = mobilenet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'MobileNetV2': x_batch = mobilenet_v2.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'DenseNet121' or network == 'DenseNet169' or network == 'DenseNet201': x_batch = densenet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif network == 'NASNetMobile' or network == 'NASNetLarge': x_batch = nasnet.preprocess_input(x_batch, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) elif 'EfficientNet' in network: x_batch = efficientnet.preprocess_input(x_batch) else: return None return x_batch
try: test_data = pd.read_csv('../PreProcessing/test_data.csv') submission = pd.read_csv('../DL3 Dataset/meta-data/sample_submission.csv') test_array = np.load("../PreProcessing/test_images_array.npz") model = load_model('../ModelCheckpoints/InceptionV3.01-0.908.hdf5') except Exception as e: print(e) exit(0) test_array = test_array['arr_0'] test_array=test_array/255 print('pre-processing input images\n') test_array = preprocess_input(test_array) test_img_name = list(test_data['img_name'].values) print('Done. Prediction started') l = [] for img_name in tqdm(submission.Image_name.values): pred = model.predict(test_array[test_img_name.index(img_name)].reshape(1, 250,250, 3)) pred[pred >= 0.5] = 1 pred[pred < 0.5] = 0 pred = (pred[0]).tolist() pred.insert(0, img_name) l.append(pred) prediction = pd.DataFrame() prediction = prediction.append(l, ignore_index=True) prediction.columns = submission.columns print(prediction.head())