def ext_feat(path): trn = load_array(path+'/results/trn.dat') val = load_array(path+'/results/val.dat') #test = load_array(path+'/results/test.dat') trn = preprocess_input(trn) val = preprocess_input(val) #test = preprocess_input(test) base_model = VGG16(weights='imagenet', include_top=False) conv_model = Model(input=base_model.input, output=base_model.get_layer('block5_conv3').output) conv_feat = conv_model.predict(trn) save_array(path+'/results/conv_feat.dat', conv_feat) conv_val_feat = conv_model.predict(val) save_array(path+'/results/conv_val_feat.dat', conv_val_feat)
def load_image(path): img_path = sys.argv[1] 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 x
def load_data(path, size=224, mode=None): img = Image.open(path) w,h = img.size if w < h: if w < size: img = img.resize((size, size*h//w)) w, h = img.size else: if h < size: img = img.resize((size*w//h, size)) w, h = img.size img = img.crop((int((w-size)*0.5), int((h-size)*0.5), int((w+size)*0.5), int((h+size)*0.5))) if mode=="original": return img if mode=="label": y = np.array(img, dtype=np.int32) mask = y == 255 y[mask] = 0 y = binarylab(y, size, 21) y = np.expand_dims(y, axis=0) return y if mode=="data": X = image.img_to_array(img) X = np.expand_dims(X, axis=0) X = preprocess_input(X) return X
def preprocess_image(image_path): # util function to open, resize and format pictures # into appropriate tensors img = load_img(image_path, target_size=(img_height, img_width)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img
def image_to_objects(img_path): model = VGG16(weights='imagenet') 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) preds = model.predict(x) return [label[1] for label in decode_predictions(preds, top=3)[0]]
def extract(self, img): with self.graph.as_default(): img = img.resize((224, 224)) # VGG must take a 224x224 img as an input img = img.convert('RGB') # Make sure img is color x = image.img_to_array(img) # To np.array. Height x Width x Channel. dtype=float32 x = np.expand_dims(x, axis=0) # (H, W, C)->(1, H, W, C), where the first elem is the number of img x = preprocess_input(x) # Subtracting avg values for each pixel feature = self.model.predict(x)[0] # (1, 4096) -> (4096, ) return feature / np.linalg.norm(feature) # Normalize
def load_img_and_preprocess(path, shape=None): img = image.load_img(path, target_size=shape) # convert image to array and preprocess for vgg x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
def preprocess(img): img4d = img.copy() img4d = img4d.astype("float64") if K.image_dim_ordering() == "th": # (H, W, C) -> (C, H, W) img4d = img4d.transpose((2, 0, 1)) img4d = np.expand_dims(img4d, axis=0) img4d = vgg16.preprocess_input(img4d) return img4d
def img_to_encoding(self, image_path): print('encoding: ', image_path) if self.vgg16_model is None: self.vgg16_model = self.create_vgg16_model() image = cv2.imread(image_path, 1) 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) return self.vgg16_model.predict(input)
def extract_feat(img_path): # weights: 'imagenet' # pooling: 'max' or 'avg' # input_shape: (width, height, 3), width and height should >= 48 input_shape = (224, 224, 3) model = VGG16(weights = 'imagenet', input_shape = (input_shape[0], input_shape[1], input_shape[2]), pooling = 'max', include_top = False) img = image.load_img(img_path, target_size=(input_shape[0], input_shape[1])) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) feat = model.predict(img) norm_feat = feat[0]/LA.norm(feat[0]) return norm_feat
def predict(path=None,img=None): """ 图片文字方向预测 """ ROTATE = [0,90,180,270] if path is not None: im = Image.open(path).convert('RGB') elif img is not None: im = Image.fromarray(img).convert('RGB') w,h = im.size xmin,ymin,xmax,ymax = int(0.1*w),int(0.1*h),w-int(0.1*w),h-int(0.1*h) im = im.crop((xmin,ymin,xmax,ymax))##剪切图片边缘,清楚边缘噪声 im = im.resize((224,224)) img = np.array(im) img = preprocess_input(img.astype(np.float32)) pred = model.predict(np.array([img])) index = np.argmax(pred,axis=1)[0] return ROTATE[index]
def extract_features(directory): model = VGG16() model.layers.pop() model = Model(inputs=model.inputs, outputs=model.layers[-1].output) print(model.summary()) features = dict() for name in listdir(directory): filename = directory + '/' + name image = load_img(filename, target_size=(224, 224)) image = img_to_array(image) image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) feature = model.predict(image, verbose=0) image_id = name.split('.')[0] features[image_id] = feature print('>%s' % name) return features
session = tf.compat.v1.Session(config=config) tf.compat.v1.keras.backend.set_session(session) # load the model model = VGG16() # redefine model to output right after the first hidden layer model = Model(inputs=model.inputs, outputs=model.layers[1].output) model.summary() # load the image with the required shape img = load_img('bird.jpg', target_size=(224, 224)) # convert the image to an array img = img_to_array(img) # expand dimensions so that it represents a single 'sample' img = expand_dims(img, axis=0) # prepare the image (e.g. scale pixel values for the vgg) img = preprocess_input(img) # get feature map for first hidden layer feature_maps = model.predict(img) # plot all 64 maps in an 8x8 squares square = 8 ix = 1 for _ in range(square): for _ in range(square): # specify subplot and turn of axis ax = pyplot.subplot(square, square, ix) ax.set_xticks([]) ax.set_yticks([]) # plot filter channel in grayscale pyplot.imshow(feature_maps[0, :, :, ix - 1], cmap='gray') ix += 1 # show the figure
def set_reference_images(self, images_list): assert (len(images_list) != 0 and len(images_list) <= self.batch_size) loaded_image = load_images(images_list, self.img_size, sharpen=self.sharpen_input) image_features = None if self.perceptual_model is not None: image_features = self.perceptual_model.predict_on_batch( preprocess_input(np.array(loaded_image))) weight_mask = np.ones(self.features_weight.shape) if self.face_mask: image_mask = np.zeros(self.ref_weight.shape) for (i, im) in enumerate(loaded_image): try: _, img_name = os.path.split(images_list[i]) mask_img = os.path.join(self.mask_dir, f'{img_name}') if (os.path.isfile(mask_img)): print("Loading mask " + mask_img) imask = PIL.Image.open(mask_img).convert('L') mask = np.array(imask) / 255 mask = np.expand_dims(mask, axis=-1) else: mask = self.generate_face_mask(im) imask = (255 * mask).astype('uint8') imask = PIL.Image.fromarray(imask, 'L') print("Saving mask " + mask_img) imask.save(mask_img, 'PNG') mask = np.expand_dims(mask, axis=-1) mask = np.ones(im.shape, np.float32) * mask except Exception as e: print("Exception in mask handling for " + mask_img) traceback.print_exc() mask = np.ones(im.shape[:2], np.uint8) mask = np.ones(im.shape, np.float32) * np.expand_dims( mask, axis=-1) image_mask[i] = mask img = None else: image_mask = np.ones(self.ref_weight.shape) if len(images_list) != self.batch_size: if image_features is not None: features_space = list(self.features_weight.shape[1:]) existing_features_shape = [len(images_list)] + features_space empty_features_shape = [self.batch_size - len(images_list) ] + features_space existing_examples = np.ones(shape=existing_features_shape) empty_examples = np.zeros(shape=empty_features_shape) weight_mask = np.vstack([existing_examples, empty_examples]) image_features = np.vstack( [image_features, np.zeros(empty_features_shape)]) images_space = list(self.ref_weight.shape[1:]) existing_images_space = [len(images_list)] + images_space empty_images_space = [self.batch_size - len(images_list) ] + images_space existing_images = np.ones(shape=existing_images_space) empty_images = np.zeros(shape=empty_images_space) image_mask = image_mask * np.vstack( [existing_images, empty_images]) loaded_image = np.vstack( [loaded_image, np.zeros(empty_images_space)]) if image_features is not None: self.assign_placeholder("features_weight", weight_mask) self.assign_placeholder("ref_img_features", image_features) self.assign_placeholder("ref_weight", image_mask) self.assign_placeholder("ref_img", loaded_image)
def image_classify(): if request.method == 'POST': enc_data = request.form[ 'img'] # enc_dataはu"data:image/png;base64,kpvdfkgspkhgsKOJ..."の形(base64) global current_image_jpeg_base64 current_image_jpeg_base64 = enc_data global image_jpeg_base64_list image_jpeg_base64_list = list_FIFO(image_jpeg_base64_list, current_image_jpeg_base64) global timestamp_list timestamp_list = list_FIFO( timestamp_list, dt.now(timezone('Asia/Tokyo')).strftime("%Y/%m/%d %H:%M:%S")) dec_data = base64.b64decode( enc_data.split(',')[1]) # "data:image/png;base64"以下を取り出す #==以下kerasによるimage classification== img = Image.open(BytesIO(dec_data)) img_resized = img.resize( (224, 224)).convert('RGB') #RGBA画像をRGB画像に変換、画素数も統一 x = image.img_to_array(img_resized) x = np.expand_dims(x, axis=0) model = VGG16(weights='imagenet') preds = model.predict(preprocess_input(x)) results = decode_predictions(preds, top=5)[0] #==以上image classification== #以下円グラフ用のjsonデータ整形 colors_list = ["#9acce3", "#70b062", "#dbdf19", "#a979ad", "#cd5638"] highlight_list = [ "#aadbf2", "#7fc170", "#ecef23", "#bb8ebf", "#e2694a" ] label_Prob_Json = [] others_value = 100 for i, result in enumerate(results): mini_dict = { 'value': "{0:0.2f}".format(round(result[2] * 100, 2)), 'color': colors_list[i], 'highlight': highlight_list[i], 'label': result[1].upper().replace('_', ' ') } label_Prob_Json.append(mini_dict) others_value -= result[2] * 100 label_Prob_Json.append({ 'value': "{0:0.2f}".format(round(others_value, 2)), 'color': '#000', 'highlight': '#000', 'label': 'OTHERS' }) global current_image_classify_result current_image_classify_result = label_Prob_Json global image_classify_result_list image_classify_result_list = list_FIFO(image_classify_result_list, current_image_classify_result) return render_template( 'index.html' ) #返り値は必ず必要、なかったら怒られる ここでrender_template('result.html')したいけど
y_test = to_categorical(y_test, num_classes=no_classes) if (flag == 0): images = np.zeros((x_train.shape[0], 224, 224, 3)) print("Loading train images") for i in range(x_train.shape[0]): image_name = path + str(x_train[i]) img = image.img_to_array(image.load_img(image_name)) images[i] = img #sd = np.std(images) #images -= mean #images /= sd print("Generating Features for train") print(images.shape) images = preprocess_input(images) features_train = inter_model.predict(images) print("Feature generation complete") images = np.zeros((x_test.shape[0], 224, 224, 3)) print("Loading test images") for i in range(x_test.shape[0]): image_name = path + str(x_test[i]) img = image.img_to_array(image.load_img(image_name)) images[i] = img #sd = np.std(images) #images -= mean #images /= sd print("Generating Features for test") print(images.shape)
def run(item, city, thedir, site, input_file=False, outdir='myflask/static/matches/', first=False, features_only=False, sold=False, model=False, pretrained_exists=False, topn=12): """ Puts everything together: loads the model, loads the features, applies cosine similarity and returns the matching 10 items Args: item: The item you want to search for (e.g., couch) city: The city where you are searching thedir: the primary directory (defined early and passes around for easily porting all programs elsewhere (e.g., AWS) Returns: Nothing, but copies matching items to a temporary directory """ K.clear_session() tf.compat.v1.reset_default_graph() tf.reset_default_graph() tf.keras.backend.clear_session() #item='couch' #city='los_angeles' #thedir='/Users/bsalmon/BrettSalmon/data_science/Insight/goodriddance/scraping/offerup/' if not sold: folder = (thedir + city + '/' + item + '_images/') features_path = (thedir + city + '/cnn/' + item + '_features/') file_mapping_path = (thedir + city + '/cnn/' + item + '_file_mapping/') if not os.path.exists( features_path.replace(features_path.split('/')[-2] + '/', '')): os.mkdir( features_path.replace(features_path.split('/')[-2] + '/', '')) if not os.path.exists(features_path): os.mkdir(features_path) if not os.path.exists(file_mapping_path): os.mkdir(file_mapping_path) else: folder = (thedir + city + '/' + item + '_images/sold/') features_path = (thedir + city + '/cnn/sold_' + item + '_features/') file_mapping_path = (thedir + city + '/cnn/sold_' + item + '_file_mapping/') if not os.path.exists(features_path): os.mkdir(features_path) if not os.path.exists(file_mapping_path): os.mkdir(file_mapping_path) model = load_headless_pretrained_model(pretrained_exists=pretrained_exists) # I'll load all images into memory because it's not that many #images=np.load(features_path+'images.npy') #image_paths=np.load(features_path+'image_paths.npy') if first or features_only: if item == 'couch': plural = 'es' else: plural = 's' print("%% You are generating the image features for all " + item + plural + " from " + city) images, image_paths = load_images(folder) #np.save(features_path+'images',images) #np.save(features_path+'image_paths',np.array(image_paths)) images_features, file_index = generate_features(image_paths, model) vector_search.save_features(features_path, images_features, file_mapping_path, file_index) if features_only: return else: print( "%% You already have the image features in hand-- loading them from disk." ) images_features, file_index = vector_search.load_features( features_path, file_mapping_path) #images=np.load(features_path+'images.npy') #image_paths=np.load(features_path+'image_paths.npy') # Define the location of the file uploaded by the user if not input_file: tfiles = os.listdir('myflask/static/uploads/') for ifile in tfiles: if ifile.endswith(".jpg"): input_file = 'myflask/static/uploads/' + ifile K.clear_session() tf.compat.v1.reset_default_graph() tf.reset_default_graph() tf.keras.backend.clear_session() model = load_headless_pretrained_model(pretrained_exists=pretrained_exists) # Load in the single input image from the user img = image.load_img(input_file, target_size=(224, 224)) x_raw = image.img_to_array(img) x_expand = np.expand_dims(x_raw, axis=0) # Extract the image features according to the headless model singleinput = preprocess_input(x_expand) single_image_features = model.predict(singleinput) # Apply cosine_similarities between features of loaded image # and features of all directory images print("%% That was fast! Applying cosine similarity and finding images") cosine_similarities = (cosine_similarity(single_image_features, images_features)[0]) # Get top N similar image ID numbers top_N_idx = (np.argsort(cosine_similarities)[-topn:])[::-1] # Get top 10 similar image files topfiles = [file_index[i] for i in top_N_idx] # Move them to a happy static folder barefiles = [] match_ids = [] for i in range(len(topfiles)): copyfile(topfiles[i], outdir + site + '/' + topfiles[i].split('/')[-1]) barefiles.append(topfiles[i].split('/')[-1]) match_ids.append(int(topfiles[i].split('/')[-1].replace('.jpg', ''))) #After prediction K.clear_session() print("%% Cosine similarity complete. Matched " + "images are in myflask/static/matches/" + site) return barefiles, match_ids, (cosine_similarities[top_N_idx])
# sys.path.append('E:\\Users\\Ross\\Downloads\\Python\\(Top)常用函數&方法\\Keras\\CAM') os.chdir("E:\\\\Users\\\\Ross\\Downloads\\Python\\(Top)常用函數&方法\\Dataset") # 讀入圖片 img_path = 'tiger.jpg' tiger = cv2.imread(img_path) tiger_origin = cv2.imread(img_path) tiger_origin = cv2.resize(tiger_origin, (224, 224)) tiger = cv2.resize(tiger, (224, 224)) # 要丟進取特徵需要resize成模型可吃的形狀 tiger = tiger.reshape(1, 224, 224, 3) tiger = np.array(tiger, dtype=np.float64) tiger = preprocess_input(tiger) # GAP def global_average_pooling(x): return K.mean(x, axis=(1, 2)) # 要對應長寬 # 載入VGG模型 model = VGG16(weights='imagenet', include_top=True) model.summary() # 接上自訂GAP層 GAP = Lambda(global_average_pooling)(model.layers[-5].output) Out = Dense(10, activation='softmax')(GAP) vgg_conv = Model(model.input, Out) vgg_conv.summary() # 抓出最後一層的GAP連softmax權重,共有Feature Map數量*最後一層Nodes個。
def on_message(client, userdata, msg): global obj, command_in, down_confirm, x_ref, y_ref, k_ref, mid_ref if msg.topic == 'image': t1 = time.time() print("message received time = " + str(t1)) with open('1.png', "wb") as fh: fh.write(base64.decodebytes(msg.payload)) info = connect.recv(1024) info = info.decode() print('Get control signal:', info) #doesn't matter from here if info == 'rec': command_in = False down_confirm = False x_ref = None y_ref = None k_ref = None print('Doing classification.') test_set = [] img_crop, img_bk = generate_crop('1.png', 220) # img_bk, k, top, mid, control_signal, x_mid = finger_control_f( '1.png', binary_thre, 5, -70, 3) #cv2.imshow('Binary Image', img_bk) cv2.waitKey(3) cv2.imwrite('2nd_step.jpg', img_crop) img = image.load_img('2nd_step.jpg', target_size=(224, 224)) img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) vgg16_feature = model.predict(img_data) test_set.append(np.ndarray.tolist(vgg16_feature[0])) #print(test_set) if test_set: predict_target = clf.predict(test_set) print(predict_target.shape) print(predict_target.size) predict_prob = clf.predict_proba(test_set) #print(correct_tag) print('predict results.') print(clf.classes_) print(predict_prob) prob = predict_prob[0] orderedIndex = sorted(range(len(prob)), key=lambda k: prob[k], reverse=True) print(orderedIndex) print("appliances in order") validNum = 0 validNum = len([i for i in prob if i > 0.075]) - 1 print('There are valid object #', validNum) # get all the results in order and loop thru print(predict_target) predict_target = predict_target[0] for indexCount in orderedIndex: print(clf.classes_[indexCount], end=" ") indexCount = 0 while True: print("orderedList ", clf.classes_[orderedIndex[indexCount]]) info_2 = connect.recv(1024) info_2 = info_2.decode() if info_2 == 'ACK': print(info_2) obj = clf.classes_[orderedIndex[indexCount]] break elif info_2 == '': print('Interrupted.') break indexCount += 1 if indexCount > 5: indexCount = 0 connect.sendall(b'ready') time.sleep(0.5) connect.sendall(b'Doing Con.') #don't care up until here elif info == 'con': t2 = time.time() #print(obj) #print('Con coming soon.') #img_bk is just image itself #top,mid is the coord of fingertip #xmid is the intercept that slope makes with frame img_bk, k, top, mid, control_signal, x_mid = finger_control_f( '1.png', binary_thre, 5, -70, 3) cv2.imwrite('../binary.png', img_bk) height, width = img_bk.shape t3 = time.time() #print(top,mid) #print(k,x_mid) if obj == 'Printer': pyautogui.press('a') elif obj == 'Coffee maker': pyautogui.press('b') elif obj == 'TV': pyautogui.press('c') elif obj == 'Door': pyautogui.press('d') elif obj == 'Minotor': pyautogui.press('e') #print('slope is ',k,'top y value is ',top,' and mid value is ', mid) #print('control signal is', control_signal) ############################## #creating reference photo and compares future images to reference image if not x_ref or not y_ref or not k_ref: x_ref = mid y_ref = top mid_ref = x_mid if mid == x_mid: direction = np.pi / 2 - 0.01 #print(top/(mid-x_mid)) else: direction = np.arctan(top / float((mid - x_mid))) k_ref = direction connect.sendall(b'Doing Con.') else: #if no finger, then sends a "down" flag # quite if control_signal == 'Down': print('down') pyautogui.press('m') if command_in: down_confirm = True time.sleep(0.01) connect.sendall(b'Doing Con.') #print(down_confirm) ##### else: command_in = True print('up') pyautogui.press('n') if mid == x_mid: direction = k_ref #print(top/(mid-x_mid)) else: direction = np.arctan(top / float((mid - x_mid))) print(direction - k_ref) print(x_mid - mid_ref) #mid_ref is xmid of the reference image #k_ref = direction is the slope #"//5" returns the integer digit of the width / 5 #if xmid coord - midref bigger than width /5 #width is 224 for this #maybe don't include the midref calculations? Moving xmid does not necessarily mean they are pointing at that box if (x_mid - mid_ref > width // 5) or (direction - k_ref > size): print('block 4') block = 8 pyautogui.press('8') elif (x_mid - mid_ref < -width // 5) or (direction - k_ref < -size): print('block 1') block = 2 pyautogui.press('2') elif (direction - k_ref < size): print('block 2') block = 4 pyautogui.press('4') elif (direction - k_ref > -size): print('block 3') block = 6 pyautogui.press('6') #### revise this part #trying to integrate using the slope of finger and finger mid to indicate block correctly #direction is angle from xmid to top,mid #quadrant 4 is actually left side #size is alpha from the diagram #add time.sleep(time) only to server if down_confirm == True: down_confirm = False command_in = False #connect.sendall(b'Stop Con.') connect.sendall(b'Doing Con.') else: connect.sendall(b'Doing Con.')
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions from keras.preprocessing import image import numpy as np import sys if len(sys.argv) != 2: print("usage: python test_vgg16.py [image file]") sys.exit(1) filename = sys.argv[1] model = VGG16(weights='imagenet') img = image.load_img(filename, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) preds = model.predict(preprocess_input(x)) results = decode_predictions(preds, top=5)[0] for result in results: print(result)
def get_image_features(self, image_directory): from keras.preprocessing import image from keras.models import Model if self.cnn_extractor == 'vgg16': from keras.applications.vgg16 import preprocess_input from keras.applications import VGG16 self.IMG_FEATS = 224*224*3 base_model = VGG16(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) self.extracted_features = [] self.image_feature_files = list(set(self.image_files)) number_of_images = len(self.image_feature_files) for image_arg, image_file in enumerate(self.image_feature_files): image_path = image_directory + image_file if image_arg%100 == 0: print('%.2f %% completed' % round(100*image_arg/number_of_images,2)) img = image.load_img(image_path, target_size=(224, 224)) img = image.img_to_array(img) # img = np.expand_dims(img, axis=0) # img = preprocess_input(img) #CNN_features = model.predict(img) self.extracted_features.append(img) self.extracted_features = np.asarray(self.extracted_features) elif self.cnn_extractor == 'vgg19': from keras.applications.vgg19 import preprocess_input from keras.applications import VGG19 self.IMG_FEATS = 4096 base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) self.extracted_features = [] self.image_feature_files = list(set(self.image_files)) number_of_images = len(self.image_feature_files) for image_arg,image_file in enumerate(self.image_feature_files): image_path = image_directory + image_file if image_arg%100 == 0: print('%.2f %% completed' % round(100*image_arg/number_of_images,2)) img = image.load_img(image_path, target_size=(224, 224)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) CNN_features = model.predict(img) self.extracted_features.append(np.squeeze(CNN_features)) self.extracted_features = np.asarray(self.extracted_features) elif self.cnn_extractor == 'inception': from keras.applications.inception_v3 import preprocess_input from keras.applications import InceptionV3 self.IMG_FEATS = 2048 base_model = InceptionV3(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('flatten').output) self.extracted_features = [] self.image_feature_files = list(set(self.image_files)) number_of_images = len(self.image_feature_files) for image_arg,image_file in enumerate(self.image_feature_files): image_path = image_directory + image_file if image_arg%100 == 0: print('%.2f %% completed' % round(100*image_arg/number_of_images,2)) img = image.load_img(image_path, target_size=(299, 299)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) CNN_features = model.predict(img) self.extracted_features.append(np.squeeze(CNN_features)) self.extracted_features = np.asarray(self.extracted_features)
def train(self, data_dir, epochs=3, callback=None): if self.busy: return 1 self.busy = True label_names = get_labels(data_dir) self.NUM_CLASSES = len(label_names) from keras.applications.vgg16 import VGG16, preprocess_input # 采用VGG16为基本模型,include_top为False,表示FC层是可自定义的,抛弃模型中的FC层;该模型会在~/.keras/models下载基本模型 base_model = VGG16(input_shape=(self.IMAGE_SIZE, self.IMAGE_SIZE, 3), include_top=False, weights='imagenet') x_data, y_label = load_img_from_dir(data_dir, target_size=(self.IMAGE_SIZE, self.IMAGE_SIZE), max_num=30) for i in range(x_data.shape[0]): x_data[i] = preprocess_input(x_data[i]) print(x_data.shape) print(x_data[0].shape) x_data = x_data.reshape(x_data.shape[0], self.IMAGE_SIZE, self.IMAGE_SIZE, 3) y_label_one_hot = prepress_labels(y_label) # 验证应该使用从未见过的图片 train_x, test_x, train_y, test_y = train_test_split(x_data, y_label_one_hot, random_state=0, test_size=0.3) # 自定义FC层以基本模型的输入为卷积层的最后一层 x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(self.FC_NUMS, activation='relu')(x) prediction = Dense(self.NUM_CLASSES, activation='softmax')(x) # 构造完新的FC层,加入custom层 model = Model(inputs=base_model.input, outputs=prediction) # 获取模型的层数 print("layer nums:", len(model.layers)) # 除了FC层,靠近FC层的一部分卷积层可参与参数训练, # 一般来说,模型结构已经标明一个卷积块包含的层数, # 在这里我们选择FREEZE_LAYERS为17,表示最后一个卷积块和FC层要参与参数训练 for layer in model.layers[:self.FREEZE_LAYERS]: layer.trainable = False for layer in model.layers[self.FREEZE_LAYERS:]: layer.trainable = True for layer in model.layers: print("layer.trainable:", layer.trainable) # 预编译模型 model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) model.summary() model.fit( train_x, train_y, validation_data=(test_x, test_y), # model.fit(x_data, y_label_one_hot, # validation_split=0.4, callbacks=[AccuracyLogger(callback)], epochs=epochs, batch_size=4, # steps_per_epoch=1,validation_steps =1 , verbose=1, shuffle=True) self.model = model model.save(os.path.join(data_dir, 'model.h5')) self.dump_label_name(label_names) self.session = K.get_session() self.graph = tf.get_default_graph() self.busy = False
def next(self): with self.lock: index_array, current_index, current_batch_size = next( self.index_generator) if self.target_size: batch_x = np.zeros((current_batch_size, ) + self.image_shape) if self.loss_shape is None and self.label_file_format is 'img': batch_y = np.zeros((current_batch_size, ) + self.label_shape, dtype=int) elif self.loss_shape is None: batch_y = np.zeros((current_batch_size, ) + self.label_shape) else: batch_y = np.zeros((current_batch_size, ) + self.loss_shape, dtype=np.uint8) grayscale = self.color_mode == 'grayscale' # build batch of image data and labels for i, j in enumerate(index_array): data_file = self.data_files[j] label_file = self.label_files[j] img_file_format = 'img' img = load_img(os.path.join(self.data_dir, data_file), grayscale=grayscale, target_size=None) label_filepath = os.path.join(self.label_dir, label_file) if self.label_file_format == 'npy': y = np.load(label_filepath) else: label = Image.open(label_filepath) if self.save_to_dir and self.palette is None: self.palette = label.palette # do padding if self.target_size: if self.crop_mode != 'none': x = img_to_array(img, data_format=self.data_format) if self.label_file_format is not 'npy': y = img_to_array( label, data_format=self.data_format).astype(int) img_w, img_h = img.size if self.pad_size: pad_w = max(self.pad_size[1] - img_w, 0) pad_h = max(self.pad_size[0] - img_h, 0) else: pad_w = max(self.target_size[1] - img_w, 0) x = np.lib.pad(x, ((pad_h / 2, pad_h - pad_h / 2), (pad_w / 2, pad_w - pad_w / 2), (0, 0)), 'constant', constant_values=0.) y = np.lib.pad(y, ((pad_h / 2, pad_h - pad_h / 2), (pad_w / 2, pad_w - pad_w / 2), (0, 0)), 'constant', constant_values=self.label_cval) else: x = img_to_array(img.resize( (self.target_size[1], self.target_size[0]), Image.BILINEAR), data_format=self.data_format) if self.label_file_format is not 'npy': y = img_to_array( label.resize( (self.target_size[1], self.target_size[0]), Image.NEAREST), data_format=self.data_format).astype(int) else: print( 'ERROR: resize not implemented for label npy file') if self.target_size is None: batch_x = np.zeros((current_batch_size, ) + x.shape) if self.loss_shape is not None: batch_y = np.zeros((current_batch_size, ) + self.loss_shape) else: batch_y = np.zeros((current_batch_size, ) + y.shape) x, y = self.seg_data_generator.random_transform(x, y) x = self.seg_data_generator.standardize(x) if self.ignore_label: y[np.where(y == self.ignore_label)] = self.classes if self.loss_shape is not None: y = np.reshape(y, self.loss_shape) batch_x[i] = x batch_y[i] = y # optionally save augmented images to disk for debugging purposes if self.save_to_dir: for i in range(current_batch_size): img = array_to_img(batch_x[i], self.data_format, scale=True) label = batch_y[i][:, :, 0].astype('uint8') label[np.where(label == self.classes)] = self.ignore_label label = Image.fromarray(label, mode='P') label.palette = self.palette fname = '{prefix}_{index}_{hash}'.format( prefix=self.save_prefix, index=current_index + i, hash=np.random.randint(1e4)) img.save( os.path.join( self.save_to_dir, 'img_' + fname + '.{format}'.format(format=self.save_format))) label.save( os.path.join(self.save_to_dir, 'label_' + fname + '.png')) # return batch_x = preprocess_input(batch_x) if self.class_mode == 'sparse': return batch_x, batch_y else: return batch_x
def read_and_preprocess_img(path, size=(224,224)): img = load_img(path, target_size=size) x = img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
def train(options): height = options['height'] width = options['width'] batch_size = options['batch_size'] learning_rate = options['learning_rate'] steps_per_epoch = options['steps_per_epoch'] epochs = options['epochs'] batch_size = options['batch_size'] verbose_iter = options['verbose_iter'] content_w = options['content_w'] style_w1 = options['style_w1'] style_w2 = options['style_w2'] style_w3 = options['style_w3'] style_w4 = options['style_w4'] tv_w = options['tv_w'] output_w = options['output_w'] style_img_path = options['style_img_path'] test_content_img_path = options['test_content_img_path'] model_saving_path = options['model_saving_path'] train_dataset_path = options['train_dataset_path'] style_layers = options['style_layers'] content_layer = options['content_layer'] net_name = options['net_name'] # Build model model = get_training_model(height, width, batch_size) # Loading imagenet weights in our VGG16 model's part and frozing model_layers = {layer.name: layer for layer in model.layers} vgg_imagenet = vgg16.VGG16(weights='imagenet', include_top=False) vgg_imagenet_layers = {layer.name: layer for layer in vgg_imagenet.layers} for layer in vgg_imagenet.layers: if layer.name in model_layers: # load imagenet weights in model layer model_layers[layer.name].set_weights( vgg_imagenet_layers[layer.name].get_weights()) # froze layer model_layers[layer.name].trainable = False # Compile model model_loss_weights = [ content_w, style_w1, style_w2, style_w3, style_w4, tv_w, output_w ] model_optimizer = optimizers.Adam(lr=learning_rate) model_loss = { 'content_loss': dummy_loss, 'style_loss1': dummy_loss, 'style_loss2': dummy_loss, 'style_loss3': dummy_loss, 'style_loss4': dummy_loss, 'tv_loss': dummy_loss, 'model_output': zero_loss } model.compile(loss=model_loss, optimizer=model_optimizer, loss_weights=model_loss_weights) print('Model succesfully compiled!') #Getting activation to fit them in our model # Get style activations style_tensor = preprocess_img(style_img_path, height, width) style_act = [] for layer_name in style_layers: style_function = get_func_extract_vgg_activations( layer_name, width, height) style_activation = expand_batch_input( batch_size, style_function([style_tensor])[0]) style_act.append(style_activation) # Get content activations for test image content_test_tensor = preprocess_img(test_content_img_path, height, width) content_function = get_func_extract_vgg_activations( content_layer, width, height) content_test_activation = expand_batch_input( batch_size, content_function([content_test_tensor])[0]) content_test = expand_batch_input(batch_size, content_test_tensor) # Training loop datagen = ImageDataGenerator() dummy_input = expand_batch_input(batch_size, np.array([0.0])) start_time = time.time() summ_time = 0 for ep in range(epochs): print('Epoch: ', ep) iters = 0 for x in datagen.flow_from_directory(train_dataset_path, class_mode=None, batch_size=batch_size, target_size=(height, width)): t1 = time.time() x = vgg16.preprocess_input(x) content_act = content_function([x])[0] history = model.fit([ x, content_act, style_act[0], style_act[1], style_act[2], style_act[3] ], [ dummy_input, dummy_input, dummy_input, dummy_input, dummy_input, dummy_input, x ], epochs=1, verbose=0, batch_size=batch_size ) #verbose = 0, we will print info manually t2 = time.time() summ_time += t2 - t1 iters += 1 if iters % verbose_iter == 0: # predict and save image on current iteration verbose_result = model.predict([ content_test, content_test_activation, style_act[0], style_act[1], style_act[2], style_act[3] ]) verbose_image = deprocess_img(verbose_result[6][0], width, height) cv2.imwrite( os.path.join( test_content_imgs_save_path, '{}_{}_{}_test_img.jpg'.format(net_name, iters, ep)), verbose_image) # print loop info print() print('Current iteration: ', iters) loss = history.history['loss'] try: improvement = (prev_loss - loss) / prev_loss * 100 prev_loss = loss print('Improvement: {}%'.format(improvement)) except: prev_loss = loss epoch_est_time = (summ_time / verbose_iter) * ( steps_per_epoch - iters) # estimted time until epoch`s end complete_est_time = start_time + ( summ_time / verbose_iter ) * steps_per_epoch * epochs # estimated time of training complition print('Expected time before the end of the epoch: ', str(datetime.timedelta(seconds=epoch_est_time))) print_test_info(verbose_result) print() print_training_loss(history) summ_time = 0 if iters > steps_per_epoch: break print('Training process is over. Saving weights...') # Saving weights after training # Create autonencoder model without frozen layers from VGG-16 pred_model = get_pred_model(height, width) # Fill pred_model by trained weights training_model_layers = {layer.name: layer for layer in model.layers} for layer in pred_model.layers: if layer.name in training_model_layers.keys(): layer.set_weights(training_model_layers[layer.name].get_weights()) pred_model.save_weights( os.path.join(model_saving_path, 'fst_{}_weights.h5'.format(net_name))) print('Weights was saved!')
def extract_VGG19(tensor): from keras.applications.vgg19 import VGG19, preprocess_input return VGG19(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
from matplotlib import pyplot as plt '''图片路径''' content_image_path = './data/buildings.jpg' style_image_path = './data/starry-sky.jpg' generate_image_path = './data/output.jpg' '''加载图片并初始化输出图片''' target_height = 512 target_width = 512 target_size = (target_height, target_width) content_image = load_img(content_image_path, target_size=target_size) content_image_array = img_to_array(content_image) content_image_array = K.variable(preprocess_input(np.expand_dims(content_image_array, 0)), dtype='float32') style_image = load_img(style_image_path, target_size=target_size) style_image_array = img_to_array(style_image) style_image_array = K.variable(preprocess_input(np.expand_dims(style_image_array, 0)), dtype='float32') generate_image = np.random.randint(256, size=(target_width, target_height, 3)).astype('float64') generate_image = preprocess_input(np.expand_dims(generate_image, 0)) generate_image_placeholder = K.placeholder(shape=(1, target_width, target_height, 3)) def get_feature_represent(x, layer_names, model): '''图片的特征图表示 参数 ----------------------------------------------
############################################################################### for chunk in range(10): inds_chunk = inds_chunks[chunk] list_of_images = [all_imnames[item] for item in inds_chunk] temp = list_of_images[0] list_of_images = [] for item in temp: item = item[0] correct_item = item.strip() list_of_images.append(correct_item) del temp ############################################################################### VGG model vgg_out_all = [] count = 0 for item in list_of_images: print(count) count += 1 image_original = cv2.imread(path_images + item) image_resized = cv2.resize(image_original,(224,224)) image_input_vgg = preprocess_input(image_resized.reshape((1, 224, 224, 3))) vgg_out = model.predict(image_input_vgg) vgg_out_all.append(vgg_out) filename = path_out + outfilenames[chunk] outfile = open(filename,'wb') pickle.dump(vgg_out_all,outfile)
from keras.preprocessing import image from keras.applications import vgg16 # keras VGG16 implementation # Load Keras' VGG16 model that was pre-trained against the ImageNet database model = vgg16.VGG16() # Load the image file, resizing it to 224x224 pixels (required by this model) img = image.load_img("bay.jpg", target_size=(224, 224)) # Convert the image to a numpy array x = image.img_to_array(img) # Add a fourth dimension (since Keras expects a list of images) x = np.expand_dims(x, axis=0) # Normalize the input image's pixel values to the range used when training the neural network x = vgg16.preprocess_input(x) # 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 = vgg16.decode_predictions(predictions) print("Top predictions for this image:") for imagenet_id, name, likelihood in predicted_classes[0]: print("Prediction: {} - {:2f}".format(name, likelihood))
def advanced_plot(model, layer_dict, chosen_layer, image_path, predict): viz_model = Model(inputs=model.inputs, outputs=layer_dict[chosen_layer].output) image = load_img(image_path, target_size=(224,224)) image = img_to_array(image) image = np.expand_dims(image, axis=0) image = preprocess_input(image) feature_maps = viz_model.predict(image) # Plotting geometry row_choice_default = 8 try: n_rows = int(input('Number rows to plot: (default = 8) ')) except ValueError: n_rows = row_choice_default col_choice_default = 8 try: n_cols = int(input('Number cols to plot: (default = 8) ')) except ValueError: n_cols = col_choice_default print("Plotting {} filters".format(n_rows * n_cols)) index = 1 for i in range(n_rows): for j in range(n_cols): ax = plt.subplot(n_rows, n_cols, index) ax.set_xticks([]) ax.set_yticks([]) plt.imshow(feature_maps[0, :, :, index-1], cmap='viridis') index += 1 plt.suptitle('Advanced Vizualization of {}'.format(chosen_layer)) plt.show() if predict: predictions = model.predict(image) predictions = predictions[0] labels_dict = create_labels_dict() prediction_list = [] for ix, certainty in enumerate(predictions): if certainty > 0.075: guess = list(labels_dict.values())[ix] print("Classification made!: {}".format(guess)) prediction_list.append(guess) original_image = load_img(image_path) ax = plt.subplot() plt.imshow(original_image) plt.subplots_adjust(right=0.68) for label_ix, label in enumerate(prediction_list): plt.figtext( 0.8, 0.25*(label_ix+1), label, horizontalalignment='center' ) plt.suptitle('Predictions') plt.axis('off') plt.show()
plt.close() # ----------------------------------------------------------------- # Part C: # layer_name = "encoded" # encoded_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) # encoded_output = encoded_layer_model.predict(df_m_rna) path = "../Results/Images/" layer_names = ["block1_pool", "block4_conv3"] image_filenames = os.listdir(path) for image_filename in image_filenames: image = load_img(path + image_filename, target_size=(224, 224)) image = img_to_array(image) image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2])) image = preprocess_input(image) for layer_name in layer_names: new_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) filter_windows = new_model.predict(image)[0, :, :, :] for i in range(filter_windows.shape[2]): filter_window = filter_windows[:, :, i] plt.figure(figsize=(10, 10)) plt.axis("off") im = plt.imshow(filter_window, aspect="auto", interpolation="nearest", cmap="Blues") cbar_ax = plt.axes([.9, 0.15, 0.05, 0.7]) cbar = plt.colorbar(im, cax=cbar_ax) os.makedirs("../Results/Description-5/images_filters/%s/" %
def build_perceptual_model(self, generator): # Learning rate global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") incremented_global_step = tf.assign_add(global_step, 1) self._reset_global_step = tf.assign(global_step, 0) self.learning_rate = tf.train.exponential_decay( self.lr, incremented_global_step, self.decay_steps, self.decay_rate, staircase=True) self.sess.run([self._reset_global_step]) generated_image_tensor = generator.generated_image generated_image = tf.image.resize_nearest_neighbor( generated_image_tensor, (self.img_size, self.img_size), align_corners=True) self.ref_img = tf.get_variable('ref_img', shape=generated_image.shape, dtype='float32', initializer=tf.initializers.zeros()) self.ref_weight = tf.get_variable('ref_weight', shape=generated_image.shape, dtype='float32', initializer=tf.initializers.zeros()) self.add_placeholder("ref_img") self.add_placeholder("ref_weight") if (self.vgg_loss is not None): vgg16 = VGG16(include_top=False, input_shape=(self.img_size, self.img_size, 3)) self.perceptual_model = Model(vgg16.input, vgg16.layers[self.layer].output) generated_img_features = self.perceptual_model( preprocess_input(self.ref_weight * generated_image)) with tf.variable_scope("ref_img_features", reuse=tf.AUTO_REUSE) as scope: self.ref_img_features = tf.get_variable( 'ref_img_features', shape=generated_img_features.shape, dtype='float32', initializer=tf.initializers.zeros()) with tf.variable_scope("features_weight", reuse=tf.AUTO_REUSE) as scope: self.features_weight = tf.get_variable( 'features_weight', shape=generated_img_features.shape, dtype='float32', initializer=tf.initializers.zeros()) self.sess.run([ self.features_weight.initializer, self.features_weight.initializer ]) self.add_placeholder("ref_img_features") self.add_placeholder("features_weight") self.loss = 0 # Original L2 loss on VGG16 features if (self.l2_vgg_loss is not None): self.loss += self.l2_vgg_loss * tf.compat.v1.losses.mean_squared_error( self.features_weight * self.ref_img_features, self.features_weight * generated_img_features) / 82890.0 # L1 loss on VGG16 features if (self.vgg_loss is not None): self.loss += self.vgg_loss * tf_custom_l1_loss( self.features_weight * self.ref_img_features, self.features_weight * generated_img_features) # + logcosh loss on image pixels if (self.pixel_loss is not None): self.loss += self.pixel_loss * tf_custom_logcosh_loss( self.ref_weight * self.ref_img, self.ref_weight * generated_image) # + MS-SIM loss on image pixels if (self.mssim_loss is not None): self.loss += self.mssim_loss * tf.math.reduce_mean( 1 - tf.image.ssim_multiscale(self.ref_weight * self.ref_img, self.ref_weight * generated_image, 1)) # + extra perceptual loss on image pixels if self.perc_model is not None and self.lpips_loss is not None: self.loss += self.lpips_loss * tf.math.reduce_mean( self.compare_images(self.ref_weight * self.ref_img, self.ref_weight * generated_image)) # + L1 penalty on dlatent weights if self.l1_penalty is not None: self.loss += self.l1_penalty * 512 * tf.math.reduce_mean( tf.math.abs(generator.dlatent_variable - generator.get_dlatent_avg()))
if __name__ == "__main__": print "constructing network..." model = VGG16(weights='imagenet', include_top=True) print "done" # Forced initialization of keras.applications.imagenet_utils.CLASS_INDEX # imagenet_utils kind of hides the CLASS_INDEX from us, that's why this hackery is necessary. _ = decode_predictions(np.zeros((1, 1000))) from keras.applications.imagenet_utils import CLASS_INDEX for filename in sys.argv[1:]: img = image.load_img(filename, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # preds = model.predict(x) # print 'Predicted:', decode_predictions(preds)[0][1] out = model.predict(x).flatten() prediction = np.argmax(out) top5 = sorted(zip(out, range(len(out))), reverse=True)[:5] # (probability, class_id) pairs. top5_probs_and_names = [ "%s = %f ," % (CLASS_INDEX[str(prediction)][1], probability) for (probability, prediction) in top5 ] top5_names = [ CLASS_INDEX[str(prediction)][1] for (probability, prediction) in top5 ]
def process_image(img): img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) result = ClassPredictor(img) return result
def build_perceptual_model(self, generator, discriminator=None): # Learning rate global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name="global_step") incremented_global_step = tf.assign_add(global_step, 1) self._reset_global_step = tf.assign(global_step, 0) self.learning_rate = tf.train.exponential_decay( self.lr, incremented_global_step, self.decay_steps, self.decay_rate, staircase=True) self.sess.run([self._reset_global_step]) if self.discriminator_loss is not None: self.discriminator = discriminator generated_image_tensor = generator.generated_image generated_image = tf.image.resize_nearest_neighbor( generated_image_tensor, (self.img_size, self.img_size), align_corners=True) self.ref_img = tf.get_variable('ref_img', shape=generated_image.shape, dtype='float32', initializer=tf.initializers.zeros()) self.ref_weight = tf.get_variable('ref_weight', shape=generated_image.shape, dtype='float32', initializer=tf.initializers.zeros()) self.add_placeholder("ref_img") self.add_placeholder("ref_weight") if (self.vgg_loss is not None): vgg16 = VGG16(include_top=False, input_shape=(self.img_size, self.img_size, 3)) self.perceptual_model = Model(vgg16.input, vgg16.layers[self.layer].output) generated_img_features = self.perceptual_model( preprocess_input(self.ref_weight * generated_image)) self.ref_img_features = tf.get_variable( 'ref_img_features', shape=generated_img_features.shape, dtype='float32', initializer=tf.initializers.zeros()) self.features_weight = tf.get_variable( 'features_weight', shape=generated_img_features.shape, dtype='float32', initializer=tf.initializers.zeros()) self.sess.run([ self.features_weight.initializer, self.features_weight.initializer ]) self.add_placeholder("ref_img_features") self.add_placeholder("features_weight") if self.perc_model is not None and self.lpips_loss is not None: img1 = tflib.convert_images_from_uint8(self.ref_weight * self.ref_img, nhwc_to_nchw=True) img2 = tflib.convert_images_from_uint8(self.ref_weight * generated_image, nhwc_to_nchw=True) self.loss = 0 # L1 loss on VGG16 features if (self.vgg_loss is not None): if self.adaptive_loss: self.loss += self.vgg_loss * tf_custom_adaptive_loss( self.features_weight * self.ref_img_features, self.features_weight * generated_img_features) else: self.loss += self.vgg_loss * tf_custom_logcosh_loss( self.features_weight * self.ref_img_features, self.features_weight * generated_img_features) # + logcosh loss on image pixels if (self.pixel_loss is not None): if self.adaptive_loss: self.loss += self.pixel_loss * tf_custom_adaptive_rgb_loss( self.ref_weight * self.ref_img, self.ref_weight * generated_image) else: self.loss += self.pixel_loss * tf_custom_logcosh_loss( self.ref_weight * self.ref_img, self.ref_weight * generated_image) # + MS-SIM loss on image pixels if (self.mssim_loss is not None): self.loss += self.mssim_loss * tf.math.reduce_mean( 1 - tf.image.ssim_multiscale(self.ref_weight * self.ref_img, self.ref_weight * generated_image, 1)) # + extra perceptual loss on image pixels if self.perc_model is not None and self.lpips_loss is not None: self.loss += self.lpips_loss * tf.math.reduce_mean( self.perc_model.get_output_for(img1, img2)) # + L1 penalty on dlatent weights if self.l1_penalty is not None: self.loss += self.l1_penalty * 512 * tf.math.reduce_mean( tf.math.abs(generator.dlatent_variable - generator.get_dlatent_avg())) # discriminator loss (realism) if self.discriminator_loss is not None: self.loss += self.discriminator_loss * tf.math.reduce_mean( self.discriminator.get_output_for( tflib.convert_images_from_uint8( generated_image_tensor, nhwc_to_nchw=True), self.stub))
from skimage.transform import resize # for resizing images data = pd.read_csv('data.csv') # reading the csv file X = [] # creating an empty array for myFile in data.Image_ID: image = plt.imread( '/home/ubuntu/caffe/data/lamda/train/' + myFile ) # /home/ubuntu/caffe/data/lamda_2/lamdaPics/*.jpg is alrready 256x256 X.append(image) X = np.array(X) # converting list to array image = [] for i in range(0, X.shape[0]): a = resize(X[i], preserve_range=True, output_shape=(224, 224)).astype(int) # reshaping to 224*224*3 image.append(a) X = np.array(image) from keras.applications.vgg16 import preprocess_input X = preprocess_input(X, mode='tf') # preprocessing the input data image_size = 224 base_model = VGG16(weights='imagenet', include_top=False, input_shape=(image_size, image_size, 3)) batch_size = 64 X = base_model.predict(X, batch_size=batch_size, verbose=0, steps=None) np.save(open('preprocessed_X.npy', 'w'), X)
import numpy as np from keras.models import Sequential from keras.layers import Dense, Activation from keras import optimizers from matplotlib import pyplot, image from keras.models import model_from_json import cv2 from keras.applications.vgg16 import preprocess_input x1 = cv2.imread("./test/fiat.jpg") x1 = cv2.resize(x1, (300, 300)) x1 = cv2.cvtColor(x1, cv2.COLOR_BGR2RGB) pyplot.imshow(x1) pyplot.show() img = preprocess_input(x1.reshape(1, 300, 300, 3)) x2 = cv2.imread("./test/volvo.jpg") x2 = cv2.resize(x2, (300, 300)) x2 = cv2.cvtColor(x2, cv2.COLOR_BGR2RGB) pyplot.imshow(x2) pyplot.show() img2 = preprocess_input(x2.reshape(1, 300, 300, 3)) # load json and create model json_file = open('modelCars.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model
def _preprocess(self, image_tensor): """Preprocess image data by modifying it directly""" vgg16.preprocess_input(image_tensor)
def load_image(img_path): data = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(data) x = np.expand_dims(x, axis=0) x = preprocess_input(x) return x
#Instantiate VGG16 and returns a vgg16 model instance vgg16_model = VGG16(weights='imagenet', include_top=False) #include_top: whether to include the 3 fully-connected layers at the top of the network. #This has to be True for classification and False for feature extraction. Returns a model instance #weights:'imagenet' means model is pre-training on ImageNet data. model = VGG16(weights='imagenet', include_top=True) model.summary() #image file name to classify image_path = 'jumping_dolphin.jpg' #load the input image with keras helper utilities and resize the image. #Default input size for this model is 224x224 pixels. img = image.load_img(image_path, target_size=(224, 224)) #convert PIL (Python Image Library??) image to numpy array x = image.img_to_array(img) print (x.shape) #image is now represented by a NumPy array of shape (224, 224, 3), # but we need to expand the dimensions to be (1, 224, 224, 3) so we can # pass it through the network -- we'll also preprocess the image by # subtracting the mean RGB pixel intensity from the ImageNet dataset #Finally, we can load our Keras network and classify the image: x = np.expand_dims(x, axis=0) print (x.shape) preprocessed_image = preprocess_input(x) preds = model.predict(preprocessed_image) print('Prediction:', decode_predictions(preds, top=2)[0])
def extract_Resnet50(tensor): from keras.applications.resnet50 import ResNet50, preprocess_input return ResNet50(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
# convert the PIL image to a numpy array # IN PIL - image is in (width, height, channel) # In Numpy - image is in (height, width, channel) numpy_image = img_to_array(original) plt.imshow(np.uint8(numpy_image)) plt.show() print('numpy array size', numpy_image.shape) # Convert the image / images into batch format # expand_dims will add an extra dimension to the data at a particular axis # We want the input matrix to the network to be of the form (batchsize, height, width, channels) # Thus we add the extra dimension to the axis 0. image_batch = np.expand_dims(numpy_image, axis=0) print('image batch size', image_batch.shape) processed_image = vgg16.preprocess_input(image_batch.copy()) vgg_model = vgg16.VGG16(weights='imagenet') inception_model = inception_v3.InceptionV3(weights='imagenet') resnet_model = resnet50.ResNet50(weights='imagenet') mobilenet_model = mobilenet.MobileNet(weights='imagenet') # get the predicted probabilities for each class predictions = resnet_model.predict(processed_image) # print predictions # convert the probabilities to class labels # We will get top 5 predictions which is the default label = decode_predictions(predictions)
def extract_InceptionV3(tensor): from keras.applications.inception_v3 import InceptionV3, preprocess_input return InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
from keras.applications.vgg16 import preprocess_input from dataset import image_iter_train from utils import load_random_imgs, show_test_samples test_data_dir = 'img/shrine_temple/test/unknown/' x_test, true_labels = load_random_imgs( test_data_dir, seed=1) x_test_preproc = preprocess_input(x_test.copy()) / 255.0 probs = model.predict(x_test_preproc) show_test_samples(x_test, probs, image_iter_train.class_indices, true_labels)
def extract_Xception(tensor): from keras.applications.xception import Xception, preprocess_input return Xception(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
# Convert the image to a numpy array image_array = image.img_to_array(img) # Add the image to the list of images images.append(image_array) # For each 'dog' image, the expected value should be 1 labels.append(1) # Create a single numpy array with all the images we loaded x_train = # Also convert the labels to a numpy array y_train = # Normalize image data to 0-to-1 range x_train = vgg16.preprocess_input(x_train) # Load a pre-trained neural network to use as a feature extractor pretrained_nn = # Extract features for each image (all in one pass) features_x = # Save the array of extracted features to a file joblib.dump(features_x, "x_train.dat") # Save the matching array of expected values to a file joblib.dump(y_train, "y_train.dat")
def get_image(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 x
from keras.applications.vgg16 import VGG16 from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.applications.vgg16 import preprocess_input from keras.applications.vgg16 import VGG16 from keras.preprocessing.image import load_img from keras.applications.vgg16 import decode_predictions model = VGG16() img = load_img('photo.jpeg', target_size=(224, 224)) loaded_img = img_to_array(img) reshaped_img = loaded_img.reshape(1, loaded_img.shape[0], loaded_img.shape[1], loaded_img.shape[2]) processed_img_for_vgg = preprocess_input(reshaped_img) predicted_obj = model.predict(processed_img_for_vgg) # Get predicted value output_result = decode_predictions(predicted_obj) print('final results with probability ', output_result[0][0])
def get_image_features(self, image_directory): from keras.preprocessing import image from keras.models import Model if self.cnn_extractor == 'vgg16': from keras.applications.vgg16 import preprocess_input from keras.applications import VGG16 self.IMG_FEATS = 4096 base_model = VGG16(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) self.extracted_features = [] self.image_feature_files = list(set(self.image_files)) number_of_images = len(self.image_feature_files) for image_arg,image_file in enumerate(self.image_feature_files): image_path = image_directory + image_file if image_arg%100 == 0: print('%.2f %% completed' % round(100*image_arg/number_of_images,2)) img = image.load_img(image_path, target_size=(224, 224)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) CNN_features = model.predict(img) self.extracted_features.append(np.squeeze(CNN_features)) self.extracted_features = np.asarray(self.extracted_features) elif self.cnn_extractor == 'vgg19': from keras.applications.vgg19 import preprocess_input from keras.applications import VGG19 self.IMG_FEATS = 4096 base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) self.extracted_features = [] self.image_feature_files = list(set(self.image_files)) number_of_images = len(self.image_feature_files) for image_arg,image_file in enumerate(self.image_feature_files): image_path = image_directory + image_file if image_arg%100 == 0: print('%.2f %% completed' % round(100*image_arg/number_of_images,2)) img = image.load_img(image_path, target_size=(224, 224)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) CNN_features = model.predict(img) self.extracted_features.append(np.squeeze(CNN_features)) self.extracted_features = np.asarray(self.extracted_features) elif self.cnn_extractor == 'inception': from keras.applications.inception_v3 import preprocess_input from keras.applications import InceptionV3 self.IMG_FEATS = 2048 base_model = InceptionV3(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('flatten').output) self.extracted_features = [] self.image_feature_files = list(set(self.image_files)) number_of_images = len(self.image_feature_files) for image_arg,image_file in enumerate(self.image_feature_files): image_path = image_directory + image_file if image_arg%100 == 0: print('%.2f %% completed' % round(100*image_arg/number_of_images,2)) img = image.load_img(image_path, target_size=(299, 299)) img = image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) CNN_features = model.predict(img) self.extracted_features.append(np.squeeze(CNN_features)) self.extracted_features = np.asarray(self.extracted_features)
test_images = [] for img_name in test.Image_ID: img = plt.imread('' + img_name) test_images.append(img) test_img = np.array(test_images) test_image = [] for i in range(0, test_img.shape[0]): a = resize(test_img[i], preserve_range=True, output_shape=(224, 224)).astype(int) test_image.append(a) test_image = np.array(test_image) # preprocessing the images test_image = preprocess_input(test_image, mode='tf') # extracting features from the images using pretrained model test_image = base_model.predict(test_image) # converting the images to 1-D form test_image = test_image.reshape(186, 7 * 7 * 512) # zero centered images test_image = test_image / test_image.max() predictions = model.predict_classes(test_image) print(predictions) for i in range(160, 171): plt.imshow(test_images[i])
le.fit([tl for tl in train_labels]) # variables to hold features and labels features = [] labels = [] # loop over all the labels in the folder count = 1 for i, label in enumerate(train_labels): cur_path = train_path + "/" + label count = 1 for image_path in glob.glob(cur_path + "/*.jpg"): img = image.load_img(image_path, target_size=image_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) feature = model.predict(x) flat = feature.flatten() features.append(flat) labels.append(label) print ("[INFO] processed - " + str(count)) count += 1 print ("[INFO] completed label - " + label) # encode the labels using LabelEncoder le = LabelEncoder() le_labels = le.fit_transform(labels) # get the shape of training labels print ("[STATUS] training labels: {}".format(le_labels)) print ("[STATUS] training labels shape: {}".format(le_labels.shape))
def call(self, inputs, mask=None): return self.__model(preprocess_input(inputs))
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) direc = 'Raw_Images/' + str(counter) + 'image.jpg' cv2.imwrite(direc, img) #img = generate_crop(file_path,240) if not control: test_set = [] img_crop, img_bk = generate_crop(direc, 220) cv2.imwrite('2nd_step.jpg', img_crop) img = image.load_img('2nd_step.jpg', target_size=(224, 224)) img_data = image.img_to_array(img) img_data = np.expand_dims(img_data, axis=0) img_data = preprocess_input(img_data) vgg16_feature = model.predict(img_data) test_set.append(np.ndarray.tolist(vgg16_feature[0])) if test_set: predict_target = clf.predict(test_set) print(predict_target.shape) print(predict_target.size) predict_prob = clf.predict_proba(test_set) #print(correct_tag) print('predict results.') print(clf.classes_) print(predict_prob) prob = predict_prob[0] orderedIndex = sorted(range(len(prob)),
def preprocess_image(image_path): img = load_img(image_path, target_size=(img_width, img_height)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img
from keras.applications.vgg16 import decode_predictions, preprocess_input from keras.preprocessing.image import load_img, img_to_array import numpy as np model = VGG16() dog = load_img('imgs/dog.jpg', target_size=(224, 224)) dog = img_to_array(dog) cat = load_img('imgs/cat.jpg', target_size=(224, 224)) cat = img_to_array(cat) goma = load_img('imgs/goma.jpeg', target_size=(224, 224)) goma = img_to_array(goma) # convert RGB2BGR and centerize dog = preprocess_input(dog) cat = preprocess_input(cat) goma = preprocess_input(goma) input_array = np.stack([dog, cat, goma]) probs = model.predict(input_array) results = decode_predictions(probs) assume_dog = results[0] assume_cat = results[1] assume_goma = results[2] print(assume_dog) print(assume_cat) print(assume_goma)