def callback(data): print(data.data) vgg16_net = VGG16(weights='imagenet', include_top=False, input_shape=(350, 350, 3)) vgg16_net.trainable = False model = Sequential() model.add(vgg16_net) # Добавляем в модель новый классификатор model.add(Flatten()) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(4)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-5), metrics=['accuracy']) model.load_weights(path + "mnist_model.h5") img = image.load_img(path + 'IMG/' + data.data, target_size=(350, 350)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) / 255 preds = model.predict(x) classes = ['duck', 'none', 'nut', 'wheel'] print(classes[np.argmax(preds)]) pub = rospy.Publisher('class', String, queue_size=2) time.sleep(1) pub.publish(data.data[4] + ' ' + str(classes[np.argmax(preds)]))
def judgement(path): """ 画像の判別を行う Parameters ---------- path : str 画像ファイルのパス Returns ------- result : float 判別結果 """ img_data = load_img(path, target_size=(224, 224)) x_test = np.array([img_to_array(img_data)]) x_test_preproc = preprocess_input(x_test.copy()) / 255. probs = np.array(model.predict_on_batch(x_test_preproc)) ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, preprocessing_function=preprocess_input) result = probs[0][0] return result
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 load_data(col): print("\nData Load Stage") # with open(path + 'class_id_emb_attr.pickle', 'rb') as handle: # class_id_emb_attr = pickle.load(handle) with open(path + 'train_img.pickle', 'rb') as handle: train_data = pickle.load(handle) if FLAGS.debug: train_data = train_data[:500] category = train_data['class_id'].unique() category_dict = dict((category[i], i) for i in range(category.shape[0])) train_img = extract_array_from_series(train_data['img']) train_img = vgg16.preprocess_input(train_img) OneHotEncoder = preprocessing.OneHotEncoder() train_label = train_data['class_id'].apply( lambda id: category_dict[id]).values train_label = OneHotEncoder.fit_transform(np.reshape(train_label, (-1, 1))).toarray() test_id = train_data['image_id'] train_data = train_img test_data = train_data valide_data = None valide_label = None return train_data, train_label, test_data, test_id, valide_data, valide_label
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 analysis(request, *args, **kwargs): """Renders the home page.""" assert isinstance(request, HttpRequest) #load image try: myfile = request.FILES['myfile'] fs = FileSystemStorage() filename = fs.save(settings.UPLOAD_DIR + '/' + myfile.name, myfile) img = tf.keras.preprocessing.image.load_img(filename, target_size=(224, 224)) # convert image to an array x = image.img_to_array(img) # expand image dimensions x = preprocess_input(x) x = np.expand_dims(x, axis=0) with graph.as_default(): K.set_session(session) rs = model.predict(x) print(rs) rs[0][0] rs[0][1] if rs[0][0] >= 0.9: result = "This lung CT image is NOT tumorous." else: result = "Warning! This lung CT image is tumorous." except: result = "Uploading error! Please upload image file..." return render(request, 'app/analysis.html', { 'title': 'Result Page', 'result': result, 'year': datetime.now().year, })
def call(self, x, training=True, ref_state=True): proc = vgg16.preprocess_input(np.copy(x)) if ref_state: return self.ref_model(proc, training=training) else: return self.tar_model(proc, training=training)
def predict(model, img): x = img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) prediction_index = np.argmax(preds[0]) return CATEGORIES[prediction_index]
def Data_preprocessing(img, target_size): if img.size != target_size: img = img.resize(target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
def extract_feature(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) features = model.predict(x) reshape_feature = features.reshape(1,-1) return reshape_feature
def predict(model, img, target_size): if img.size != target_size: img = img.resize(target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return preds[0]
def preprocess_image(im_path, im_size, model_name): im = image.load_img(im_path, target_size=(im_size[0], im_size[1])) im = image.img_to_array(im) im = np.expand_dims(im, axis=0) if model_name == 'inception_v3': im = inception_v3.preprocess_input(im) elif model_name == 'resnet50': im = resnet50.preprocess_input(im) elif model_name == 'vgg16': im = vgg16.preprocess_input(im) return im
def get_features(img_path): img = image.load_img(img_path, target_size=(224, 224), interpolation='bicubic', color_mode='rgb') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x, mode='caffe') fc2_features = model_extractfeatures.predict(x) fc2_features = np.squeeze(fc2_features) return fc2_features
def get_feature_4096 (model, img_path): ''' # vgg16 = VGG16(weights= 'imagenet', include_top= False) # output: 7*7*512 # vgg16 = VGG16(weights= 'imagenet', include_top= True) # output: 1000*1 ''' model_4096 = Model(inputs=model.input, outputs=model.get_layer('fc2').output) 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 model_4096.predict(x)
def post(self): try: result = request.json print(result) url = result['imageURL'] images_count = int(result['imagesCount']) image_name = 'query_image.jpg' images_path = Path.cwd().parent / 'reactUI/flickr-react/src/images' address = str(images_path) + '/' + image_name urllib.request.urlretrieve(url, address) img = image.load_img(address, target_size=(224, 224), interpolation='bicubic', color_mode='rgb') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x, mode='caffe') with graph.as_default(): fc2_features = model_extractfeatures.predict(x) fc2_features = np.squeeze(fc2_features) query = np.array_split(fc2_features, 16) comp = [] dist = {} for i, sub in zip(range(16), query): calc = Results.calculate_closest(fin_centroids, sub, str(i)) dist[str(i)] = calc[1] comp.append(calc[0]) print(time.ctime()) md5s = Results.apply_async_with_callback((np.asarray(comp), dist), images_count) print(time.ctime()) yolo = Results.find_min_local(images_count, md5s) finals = [] print(yolo) print(time.ctime()) for md5 in yolo: finals.append(collection.find_one({"md5": str(md5[1])})['url']) example = finals print(example) return {'searchImage': address, 'similarImagesList': example}, 200 except Exception as error: return {"status", "Could not find similar images"}, 500
def preprocess_image_scale(image_path, img_size=None): ''' Preprocess the image scaling it so that its larger size is max_size. This function preserves aspect ratio. ''' img = load_img(image_path) if img_size: scale = float(img_size) / max(img.size) new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1]))) img = img.resize(new_size, resample=Image.BILINEAR) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img
def brainmodel_predict(img_path, model): img = image.load_img(img_path, target_size=(224, 224)) # Preprocessing the image x = image.img_to_array(img) # x = np.true_divide(x, 255) x = np.expand_dims(x, axis=0) # Be careful how your trained model deals with the input # otherwise, it won't make correct prediction! images = preprocess_input(x) # images = np.vstack([x]) preds = model.predict(images, batch_size=16) return preds
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 preprocess_image_crop(image_path, img_size): ''' Preprocess the image scaling it so that its smaller size is img_size. The larger size is then cropped in order to produce a square image. ''' img = load_img(image_path) scale = float(img_size) / min(img.size) new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1]))) # print('old size: %s,new size: %s' %(str(img.size), str(new_size))) img = img.resize(new_size, resample=Image.BILINEAR) img = img_to_array(img) crop_h = img.shape[0] - img_size crop_v = img.shape[1] - img_size img = img[crop_h:img_size+crop_h, crop_v:img_size+crop_v, :] img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img
def fetch_img_path(self, img_path_list, path, keras_img_processing): images = [] labels = [] for img_path in img_path_list: if self.__check_ext(img_path): img_abs_path = os.path.abspath(os.path.join(path, img_path)) if keras_img_processing: image = load_img(img_abs_path, target_size=(constant.IMG_WIDTH, constant.IMG_HEIGHT)) image = img_to_array(image) image = Common.reshape_from_img(image) image = preprocess_input(image) else: image = io.imread(img_abs_path, as_gray=False) label = self.process_label(img_path) images.append(image) labels.append(label) return images, labels
def extract_vgg16_features_live(self, model, video_input_file_path): print('Extracting frames from video: ', video_input_file_path) vidcap = cv2.VideoCapture(video_input_file_path) success, image = vidcap.read() features = [] success = True count = 0 while success: vidcap.set(cv2.CAP_PROP_POS_MSEC, (count * 1000)) # added this line success, image = vidcap.read() # print('Read a new frame: ', success) if success: img = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) input = img_to_array(img) input = np.expand_dims(input, axis=0) input = preprocess_input(input) feature = model.predict(input).ravel() features.append(feature) count = count + 1 x_samples = np.array(features) return x_samples
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 create_test_data(): testing_data = [] sortedTest = [] label = 1 for folder in os.listdir(TEST_DIR): sortedTest.append(folder) sortedTest.sort() for folder in tqdm(sortedTest): print(folder) folder_path = os.path.join(TEST_DIR, folder) for img in os.listdir(folder_path): img_path = os.path.join(folder_path, img) img_data = image.load_img(img_path, target_size=(IMG_SIZE, IMG_SIZE)) img_data = image.img_to_array(img_data) img_data = preprocess_input(img_data) # by7wlha gbr w y3mlha scale testing_data.append(np.array([img_data, label])) # el img el resized-label label = label + 1 np.save('test_data.npy', testing_data) return testing_data
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]))
def analysis(request, *args, **kwargs): # load image img_path = request.FILES['myfile'] img = tf.keras.preprocessing.image.load_img(img_path, target_size=(224, 224)) # convert image to an array x = image.img_to_array(img) # expand image dimensions x = preprocess_input(x) x = np.expand_dims(x, axis=0) with graph.as_default(): tf.keras.backend.set_session(session) rs = model.predict(x) print(rs) rs[0][0] rs[0][1] if rs[0][0] == 1: result = "This image is NOT tumorous." else: result = "Warning! This image IS tumorous." return render(request, 'analysis.html', {'result': result})
def get_feature_4096(model, img_path, need_crop=True, need_resize=True): ''' vgg16 = VGG16(weights= 'imagenet', include_top= False) # output: 7*7*512 vgg16 = VGG16(weights= 'imagenet', include_top= True) # output: 1000*1 Return: 4096*1 ''' model_4096 = Model(inputs=model.input, outputs=model.get_layer('fc2').output) im = Image.open(img_path) if need_crop == True: im = im.crop((70, 45, 760, 390)) if need_resize == True: im = im.resize((224, 224)) x = image.img_to_array(im) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # pdb.set_trace() return model_4096.predict(x).reshape(4096)
from tensorflow.python.keras.preprocessing import image from tensorflow.python.keras.applications.vgg16 import preprocess_input from tensorflow.python.keras.preprocessing.image import ImageDataGenerator from tensorflow.python.keras.models import Model from tensorflow.python.keras.optimizers import Adam from PIL import Image import warnings warnings.filterwarnings('ignore') from tensorflow.python.keras.models import load_model model = load_model('/root/glioAI/glioai/models/tumor_prediction.h5') # route to any of the labaled malignant images that model hasn't seen before img_path = ('/root/glioAI/data/tumortest/8 no.jpg') img = tf.keras.preprocessing.image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) img_data = preprocess_input(x) # make prediction rs = model.predict(img_data) print(rs) rs[0][0] rs[0][1] if rs[0][0] >= 0.9: prediction = 'This image is NOT tumorous.' elif rs[0][0] < 0.9: prediction = 'Warning! This image IS tumorous.' print(prediction)
def call(self, x, training=True): x = vgg16.preprocess_input(x) return self.__model(x, training=training)
def preprocess_img(img_series): return vgg16.preprocess_input(extract_array_from_series(img_series))
from tensorflow_serving.apis import prediction_service_pb2_grpc _IMAGE_URL = 'https://static1.squarespace.com/static/5b19f01b4eddec0b62c9fac1/t/5c0ad6c1575d1fa80730173e/1544214218159/animal-collar-dog-8081.jpg' image_url = _IMAGE_URL 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]]))