def parse_data(self, path, keras_mode=False): print(keras_mode) file_list = glob(os.path.join(path, 'figs_0', '*.png')) ids = list(set([x[:-4].split(os.sep)[-1] for x in file_list])) # ids.remove('Thumb') images = [] for id in tqdm(ids): image_path = [ x for x in file_list if x.find(id) > -1 and x.endswith('png') ][0] if keras_mode: img = imread(image_path, mode='RGB') img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) else: img = imread(image_path) img = img / 255 img = img.reshape([self.imsize, self.imsize, 1]) images.append(img) return images, ids
def G_CAM(self, img_path, IMG_HIGH, IMG_WIDTH, layer_name): IMG = image.load_img(img_path, target_size=(IMG_HIGH, IMG_WIDTH)) plt.show() img = image.img_to_array(IMG) img = np.expand_dims(img, axis=0) img = preprocess_input(img) predictions = self.model.predict_generator(self.data.test_generator) filenames = self.data.test_generator.filenames print(filenames) r = np.argmax(predictions, axis=-1) # multiple categories print('label: ', r) img_output = self.model.output[:, r] conv_layer = self.model.get_layer(layer_name) # conv_layer = model.layers[1].get_layer(layer_name) grads = k.gradients(img_output, conv_layer.output)[0] print('grads shape:', grads.shape) pooled_grads = k.mean(grads, axis=(0, 1, 2)) print('mean gradiant: ', pooled_grads.shape) iterate = k.function([self.model.input], [pooled_grads, conv_layer.output[0]]) pooled_grades_value, conv_layer_output_value = iterate([img]) print(pooled_grades_value.shape) print(conv_layer_output_value.shape) output = conv_layer_output_value[0, :] weights = pooled_grades_value #np.mean(grads_val, axis=(0, 1)) cam = np.ones(output.shape[0:2], dtype=np.float32) for i, w in enumerate(weights): cam += w * output[:, :, i] cam /= np.max(cam) w, h, j = IMG.shape cam = cv2.resize(cam, (h, w)) heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_RAINBOW) # print(heatmap) heatmap[np.where(cam < 0.02)] = 0 # img = heatmap * 0.5 + img img = img[0, :, :, :] print(heatmap.shape) print(IMG.shape) img = cv2.addWeighted(heatmap, 0.5, IMG, 0.9, 0) plt.imshow(heatmap) plt.show() plt.imshow(img) plt.show()
def extract_features(self, input_image): ''' :param input_image: RGB image array of size w x h x 3 :return: 1-D feature vector of size: 7x7x512 = 25,088 ''' img_data = preprocess_input(input_image) features = self.model.predict(img_data) features = features.flatten() return features
def display_heatmap(self, img_path, img_high, img_width, layer_name, load_data): IMG = image.load_img(img_path, target_size=(img_high, img_width)) plt.show() img = image.img_to_array(IMG) img = np.expand_dims(img, axis=0) img = preprocess_input(img) hmap = heatmap() img_heatmap = hmap.create_heat_map(self.model, layer_name, img, img_high, img_width, 3, load_data) print('heatmap') plt.imshow(img_heatmap) plt.show() img_heatmap = hmap.super_impose_heatmap(img_path, img_heatmap) plt.imshow(img_heatmap) plt.show()
def preprocess_input(*args, **kwargs): return vgg16.preprocess_input(*args, **kwargs)
x = (width - square_size) // 2 y = (height - square_size) // 2 image = image.crop((x, y, square_size, square_size)) plt.imshow(np.asarray(image)) # Resizes to the networks required input size. target_square_size = max(dimension for dimension in model.layers[0].input_shape if dimension) image = image.resize((target_square_size, target_square_size), Image.ANTIALIAS) plt.imshow(np.asarray(image)) numpy_image = img.img_to_array(image) print(numpy_image.shape) image_batch = np.expand_dims(numpy_image, axis=0) print(image_batch.shape) pre_processed = preprocess_input(image_batch, data_format='channels_last') print(pre_processed.shape) features = model.predict(pre_processed) print(features.shape) import pprint pprint.pprint(decode_predictions(features, top=10)) FLCIKR_KEY = os.environ['FLICKR_KEY'] FLICKR_SECRET = os.environ['FLICKR_SECRET'] flickr = flickrapi.FlickrAPI(FLCIKR_KEY, FLICKR_SECRET, format='parsed-json') results = flickr.photos.search(text='cat', per_page='10', sort='relevance')
https://colab.research.google.com/drive/1kLOtfIlx8gpJamwqZSXZiJ67Tz7f1Pah """ import tensorflow from keras_preprocessing.image import load_img, img_to_array from keras_applications.vgg16 import preprocess_input, decode_predictions, VGG16 model = VGG16() images = load_img("speaker.jpg", target_size=(224, 224)) img_array = img_to_array(images) img_array img_array = img_array.reshape( (1, img_array.shape[0], img_array.shape[1], img_array.shape[2])) img_array.shape image = preprocess_input(img_array) image img_pred = model.predict(image) d_img_pred = decode_predictions(img_pred) d_img_pred
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
def get_labels(output): predicted_class_indices = np.argmax(output, axis=1) predicted_classes = [ 'Real' if x == 0 else 'Barca' for x in predicted_class_indices ] return predicted_classes test_dir = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data 2\\1st half test\\' images = np.load(test_dir + 'test_images.npy') labels = np.load(test_dir + 'test_labels.npy') X_test = [] for image in images: cv2.rectangle(image, (40, 16), (70, 21), (255, 255, 255), 10) X_test += [preprocess_input(image)] X_test = np.array(X_test) X_test = X_test[labels != 'Other'] labels = labels[labels != 'Other'] real_images = X_test[labels == 'Real'] barca_images = X_test[labels == 'Barca'] print('real_images.shape=', real_images.shape) model = TeamClf() model = model.model for layer in model.layers[0].layers: layer.trainable = False saved_weights_path = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\fine_tuning_approach\\transfer\\weights\\' model.load_weights(saved_weights_path + 'weights-improvement-21-0.85.hdf5')