def log_reg_predict_one_image(filename, log_reg_model, scaler, encode_tags, popular_tags): vgg_model = VGG16() vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-1].output) image = load_img(filename, target_size=(224, 224)) image = img_to_array(image) # (224, 224, 3) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) feature = vgg_model.predict(image, verbose=0) label = decode_predictions(feature)[0][0][1].replace("_", " ") encode_prediction = embed([label])[0] encode_cross_popular_tag = [] for i in range(len(encode_tags)): encode_cross_popular_tag.append(encode_tags[i] * encode_prediction) encode_cross_popular_tag = scaler.transform( encode_cross_popular_tag) # standardization probability_table = log_reg_model.predict_proba( encode_cross_popular_tag)[:, 1] # probability that the label is 1 index_list = sorted(range(len(probability_table)), key=lambda k: probability_table[k])[ -12:] # 12 index with the largest probability final_tag_prediction = [ "#" + popular_tags[i].replace(" ", "") for i in index_list ] return final_tag_prediction
def get_classinfo(model, img_path): img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return decode_predictions(model.predict(x), top=3)[0]
def predict(image_path, verbose=1): # Load and resize the image using PIL. img = PIL.Image.open(image_path) img_resized = img.resize(input_shape, PIL.Image.LANCZOS) if (verbose != 0): # Plot the image. plt.imshow(img_resized) plt.show() # Convert the PIL image to a numpy-array with the proper shape. img_array = np.expand_dims(np.array(img_resized), axis=0) # Use the VGG16 model to make a prediction. # This outputs an array with 1000 numbers corresponding to # the classes of the ImageNet-dataset. pred = model.predict(img_array) # Decode the output of the VGG16 model. pred_decoded = decode_predictions(pred, top=1000) # Print the predictions. if (verbose != 0): for code, name, score in pred_decoded: print("{0:>6.2%} : {1}".format(score, name)) return pred_decoded
def predict(image_path): img = PIL.Image.open(image_path) img_resized = img.resize(input_shape, PIL.Image.LANCEOS) plt.imshow(img_resized) plt.show() img_array = np.expand_dims(np.array(img_resized), axis=0) pred = model.predict(img_array) pred_decoded = decode_predictions(pred)[0] for code, name, score in pred_decoded: print('{0:>6.2%}: {1}'.format(score, name))
def euclidean_predict_one_image(filename, tree, popular_tags): vgg_model = VGG16() vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-1].output) image = load_img(filename, target_size=(224, 224)) image = img_to_array(image) # (224, 224, 3) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) feature = vgg_model.predict(image, verbose=0) label = decode_predictions(feature)[0][0][1].replace("_", " ") print(label) encode_prediction = embed([label])[0] euclidean_final_tag_prediction = [ "#" + popular_tags[i].replace(" ", "") for i in tree.query(encode_prediction, k=12)[1] ] return euclidean_final_tag_prediction
def predict(image_path): # Load and resize the image using PIL. img = PIL.Image.open(image_path) img_resized = img.resize(input_shape, PIL.Image.LANCZOS) # Plot the image. plt.imshow(img_resized) plt.show() # Convert the PIL image to a numpy-array with the proper shape. img_array = np.expand_dims(np.array(img_resized), axis=0) # Use the VGG16 model to make a prediction. # This outputs an array with 1000 numbers corresponding to # the classes of the ImageNet-dataset. pred = model.predict(img_array) # Decode the output of the VGG16 model. pred_decoded = decode_predictions(pred)[0] pred_name = '' pred_score = 0 count = 0 # Print the predictions. for code, name, score in pred_decoded: print("{0:>6.2%} : {1}".format(score, name)) if(count == 0): pred_name = name pred_score = "{0:>6.2%}".format(score) count = count + 1 img_name = image_path.split('/')[-1] data = { 'Image_name': img_name, 'class_name': pred_name, 'score': pred_score } array.append(data) appendXml(array)
def make_vgg_predictions(directory, image_to_tag_mapping_keys): vgg_model = VGG16() vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-1].output) vgg_predictions = dict() temp = 1 for name in os.listdir(directory): if temp % 100 == 0: print("CNN prediction in progress: " + str(temp) + " done") temp += 1 filename = directory + name name = name.split('.')[0] if name not in image_to_tag_mapping_keys: continue image = load_img(filename, target_size=(224, 224)) image = img_to_array(image) # (224, 224, 3) image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) feature = vgg_model.predict(image, verbose=0) label = decode_predictions(feature)[0] vgg_predictions[name] = label[0][1].replace("_", " ") return vgg_predictions
def predict(): model = VGG16() # print(model.summary()) # 1,加载图片 image = load_img('./tiger.jpg', target_size=(224, 224)) image = img_to_array(image) print(image.shape) # 2. 转换数据,使其满足卷积神经网络的要求, 卷积神经网络要求的输入是4个维度,第一个维度是 # 输入图片的数量 image = image.reshape(1, image.shape[0], image.shape[1], image.shape[2]) # 使用接口对数据进行归一化等处理 image = preprocess_input(image) # 处理完成之后开始进行预测 prediction = model.predict(image) # 打印预测结果 print(prediction) # 对预测结果进行解码 label = decode_predictions(prediction) # 打印解码后的结构 print(label) # 根据解码结果,取出想要的内容 print("预测的结果是:{}, 预测的概率是:{}".format(label[0][0][1], label[0][0][2]))
filename = image_url.split('/')[-1] urllib.request.urlretrieve(image_url, filename) # Load the image and resize to (224, 224) image = load_img(filename, target_size=(224, 224)) # Add channels (224, 224, 3) image = img_to_array(image) # Scale pixels for Tensorflow image = preprocess_input(image) # Batch size will be 1 image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) server = 'localhost:8500' # gRPC port channel = grpc.insecure_channel(server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) request = predict_pb2.PredictRequest() request.model_spec.name = 'resnet_v1_50_fp32' # Model name from example request.model_spec.signature_name = 'predict' # See saved_model_cli show command request.inputs['input'].CopyFrom( tf.compat.v1.make_tensor_proto(image, dtype=tf.float32, shape=[1, image.shape[1], image.shape[2], image.shape[3]])) result = stub.Predict(request, 30.0) # 30 secs timeout probabilities = numpy.array(result.outputs['probabilities'].float_val) probabilities = numpy.resize(probabilities, [1, 1000]) print("predictions: ", decode_predictions(probabilities, top=3)[0]) # Convert probabilities to class labels
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') photos = results['photos']['photo'] pprint.pprint(results)
from tensorflow.python.keras.applications.vgg16 import preprocess_input from tensorflow.python.keras.applications.vgg16 import decode_predictions from tensorflow.python.keras.preprocessing.image import load_img, img_to_array import numpy as np imgpath = 'C:/Users/user/Desktop/workspace/doc/sysp/img' #モデル作成 model = VGG16() #model.summary() #画像ロード pillow cat_img = load_img('{}/cat.jpg'.format(imgpath), target_size=(224, 224)) dog_img = load_img('{}/dog.jpg'.format(imgpath), target_size=(224, 224)) #画像を数値変更 train_dog_nd = img_to_array(dog_img) train_cat_nd = img_to_array(cat_img) #前処理 train_dog_img = preprocess_input(train_dog_nd) train_cat_img = preprocess_input(train_cat_nd) #画像を配列にまとめ train_img = np.stack([train_dog_img, train_cat_img]) probs = model.predict(train_img) resutlt = decode_predictions(probs) ind = np.argmax(resutlt[0]) print(ind)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' from tensorflow.python.keras.applications.vgg16 import VGG16 from tensorflow.python.keras.applications.vgg16 import preprocess_input, decode_predictions from tensorflow.python.keras.preprocessing import image import numpy as np # Step 1: Preprocess data img = image.load_img("images/car.jpg", target_size=(224, 224)) # Convert the image to a numpy array x = image.img_to_array(img) # Add a forth dimension since Keras expects a list of images x = np.expand_dims(x, axis=0) # Step 2: Load Pre-trained Model model = VGG16() print(model.summary()) # # Run the image through the deep neural network to make a prediction predictions = model.predict(x) # Look up the names of the predicted classes. Index zero is the results for the first image. predicted_classes = decode_predictions(predictions, top=3) print("This is an image of:") for imagenet_id, name, likelihood in predicted_classes[0]: print(" - {}: {:2f} likelihood".format(name, likelihood * 100))
arr_dog = img_to_array(img_dog) arr_cat = img_to_array(img_cat) #VGG16の訓練データと一致する型に変更 preprocess_input arr_dog = preprocess_input(arr_dog) arr_cat = preprocess_input(arr_cat) #画像をまとめて、2枚画像を含む配列の入力データに変換 arr_input = np.stack([arr_dog, arr_cat]) print(arr_input.shape) # In[21]: #予測処理 probs = model.predict(arr_input) print(probs.shape) # In[32]: #予測結果をクラス名付けて、表示する from tensorflow.python.keras.applications.vgg16 import decode_predictions result = decode_predictions(probs, top=10) result[0] # In[29]: #dog result[0] #cat result[1] # In[ ]:
def run(self): if self.vgg is None and self.cnn is None: self.create_processors() dict_result = {} categorie = [ 'book_jacket', 'web_site', 'monitor', 'scoreboard', 'street_sign', 'perfume', 'carton', 'digital_clock', 'hair_spray', 'wall_clock' ] pino = "photo_downloaded\\" mypath2 = "C:\\Users\\matti\\git\\ProgettoLube\\ProgettoLube\\WebInspector\\" paths = [ os.path.join("photo_downloaded\\", fn) for fn in next(os.walk("photo_downloaded\\"))[2] ] temp = [] counter = 0 counterLubeCreoERRATI = 0 counterLubeCreoNi = 0 counterLubeCreoOk = 0 counterLubeERRATI = 0 counterLubeNi = 0 counterLubeOk = 0 counterCreoERRATI = 0 counterCreoNi = 0 counterCreoOk = 0 counterCompetitos = 0 counterNotLogo = 0 for x in paths: temp.append( "C:\\Users\\matti\\git\\ProgettoLube\\ProgettoLube\\WebInspector\\" + x) dict_result['foto_trovate'] = len(temp) for x in temp: try: image = load_img(x, target_size=(224, 224)) except PIL.UnidentifiedImageError as e: print('error') # plt.imshow(image) # plt.show() # im = cv2.resize(cv2.imread(IMAGE_PATH), (224, 224)) # il metodo predict si attende un tensore N, 224, 224, 3 # quindi per una sola immagine deve essere 1, 224, 224, 3 # im = np.expand_dims(im, axis=0) # altro modo di procedere image = np.array(image) try: image = np.expand_dims(image, axis=0) except ValueError: print('error') try: predictions = self.vgg.predict(image) except ValueError: print('error') label = decode_predictions(predictions, top=5) # retrieve the most likely result, e.g. highest probability # print(label) label = label[0][0] # label = label[0][:] # print(label) # print the classification print('%s (%.2f%%)' % (label[1], label[2] * 100)) for y in categorie: if label[1] == y: counter = counter + 1 print("LOGO CORRETTO TROVATO ", x) predizione = self.cnn.predict(x) if predizione == 'lube&creo ERRATI': counterLubeCreoERRATI = counterLubeCreoERRATI + 1 if predizione == 'lube&creo loghi ok ma proporzioni o abbinamenti NON CORRETTI': counterLubeCreoNi = counterLubeCreoNi + 1 if predizione == 'lube&creo TUTTO OK': counterLubeCreoOk = counterLubeCreoOk + 1 if predizione == 'creo ERRATI': counterCreoERRATI = counterCreoERRATI + 1 if predizione == 'creo loghi ok ma proporzioni o abbinamenti NON CORRETTI': counterCreoNi = counterCreoNi + 1 if predizione == 'creo TUTTO OK': counterCreoOk = counterCreoOk + 1 if predizione == 'lube loghi ok ma proporzioni o abbinamenti NON CORRETTI': counterLubeNi = counterLubeNi + 1 if predizione == 'lubeERRATI': counterLubeERRATI = counterLubeERRATI + 1 if predizione == 'lubeTUTTO OK': counterLubeOk = counterLubeOk + 1 if predizione == 'NOT LOGO': counterNotLogo = counterNotLogo + 1 if predizione == 'competitors': ocr_reader = OCReader flag = ocr_reader.search_for_competitors(path=x) if flag: counterCompetitos = counterCompetitos + 1 print(counter) dict_result['logo_correctness'] = { 'lube&creo ERRATI': counterLubeCreoERRATI, 'lube&creo loghi ok ma proporzioni o abbinamenti NON CORRETTI': counterLubeCreoNi, 'lube&creo TUTTO OK': counterLubeCreoOk, 'lube ERRATI': counterLubeERRATI, 'lube loghi ok ma proporzioni o abbinamenti NON CORRETTI': counterLubeNi, 'lube TUTTO OK': counterLubeOk, 'creo ERRATI': counterCreoERRATI, 'creo loghi ok ma proporzioni o abbinamenti NON CORRETTI': counterCreoNi, 'creo TUTTO OK': counterCreoOk, 'competitors': counterCompetitos, 'not logo': counterNotLogo } return dict_result
def main(unused_argv): # Setup data paths data_directory = r'E:\DataSets\CatsAndDogs\tensorflow' img_path = data_directory + r'\train_small\cat\cat.43.jpg' heat_map_path = data_directory + r'\tabby_cat_heatmap.jpg' # load full vgg16 model. model = applications.VGG16(weights='imagenet') # Print pretrained architecture. print(model.summary()) # `img` is a PIL image of size 224x224 img = image.load_img(img_path, target_size=(224, 224)) # `x` is a float32 Numpy array of shape (224, 224, 3) x = image.img_to_array(img) # We add a dimension to transform our array into a "batch" # of size (1, 224, 224, 3) x = np.expand_dims(x, axis=0) # Finally we preprocess the batch # (this does channel-wise color normalization) x = preprocess_input(x) # Print top 3 predictions. preds = model.predict(x) print('Predicted:', decode_predictions(preds, top=3)[0]) # Get top1 prediction class index # This is the "tabby" entry in the prediction vector top1 = np.argmax(preds[0]) tabby_cat_output = model.output[:, top1] # This is the output feature map of the `block5_conv3` layer, # the last convolutional layer in VGG16 last_conv_layer = model.get_layer('block5_conv3') # This is the gradient of the "tabby" class with regard to # the output feature map of `block5_conv3` grads = tf.keras.backend.gradients(tabby_cat_output, last_conv_layer.output)[0] # This is a vector of shape (512,), where each entry # is the mean intensity of the gradient over a specific feature map channel pooled_grads = tf.keras.backend.mean(grads, axis=(0, 1, 2)) # This function allows us to access the values of the quantities we just defined: # `pooled_grads` and the output feature map of `block5_conv3`, # given a sample image iterate = tf.keras.backend.function( [model.input], [pooled_grads, last_conv_layer.output[0]]) # These are the values of these two quantities, as Numpy arrays, pooled_grads_value, conv_layer_output_value = iterate([x]) # We multiply each channel in the feature map array # by "how important this channel is" with regard to the "tabby" class. for i in range(512): conv_layer_output_value[:, :, i] *= pooled_grads_value[i] # The channel-wise mean of the resulting feature map # is our heatmap of class activation. heatmap = np.mean(conv_layer_output_value, axis=-1) # Normalize heatmap between 0 and 1 for visualization. heatmap = np.maximum(heatmap, 0) heatmap /= np.max(heatmap) plt.matshow(heatmap) plt.show() # We use cv2 to load the original image img = cv2.imread(img_path) # We resize the heatmap to have the same size as the original image heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0])) # We convert the heatmap to RGB heatmap = np.uint8(255 * heatmap) # We apply the heatmap to the original image heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) # 0.4 here is a heatmap intensity factor superimposed_img = heatmap * 0.4 + img # Save the image to disk cv2.imwrite(heat_map_path, superimposed_img)
model.summary() # Load and re-size the images for network input img_dog = load_img('img/dog.jpg', target_size=(224, 224)) img_cat = load_img('img/cat.jpg', target_size=(224, 224)) # Exchange dog/cat image from Pillow data format to numpy.ndarray. arr_dog = img_to_array(img_dog) arr_cat = img_to_array(img_cat) # Centering img color channel and change the order of them arr_dog = preprocess_input(arr_dog) arr_cat = preprocess_input(arr_cat) # Merge the img for network input as array. arr_input = np.stack([arr_dog, arr_cat]) # Check the shape of input data. print('Shape of arr_input:', arr_input.shape) # Prediction of input images. probs = model.predict(arr_input) print('Shape of probs:', probs.shape) print(probs) # Decode prediction to class name and pick up 1-5 classes by high percentage order. results = decode_predictions(probs) print(results[0]) print(results[1])
https://deep-i.net """ import cv2 from tensorflow.python.keras.applications.vgg16 import VGG16 from tensorflow.python.keras.preprocessing.image import img_to_array from tensorflow.python.keras.applications.vgg16 import preprocess_input, decode_predictions # loading weights from pre-trained model model = VGG16(weights='imagenet') # loading images for classification image = cv2.imread("sample/cat.jpg") image = cv2.resize(image, dsize=(224, 224)) image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) # class prediction yhat = model.predict(image) label = decode_predictions(yhat) label = label[0][0] # result print("%s (%.2f%%)" % (label[1], label[2] * 100)) # display image image = cv2.imread("sample/cat.jpg") cv2.imshow(label[1], image) cv2.waitKey()