def extract_features(images_path): dropoutPro = 1 classNum = 1000 skip = [] num_data = 20015 batch_size = 256 images = tf.placeholder("float", [None, 224, 224, 3]) model = VGG19(images, dropoutPro, classNum, skip) feats = model.fc7 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) model.loadModel(sess) feat_all = [] index = np.linspace(0, num_data - 1, num_data).astype(int) img_idxs = [] for i in range(num_data // batch_size + 1): print(i) ind = index[i * batch_size:min((i + 1) * batch_size, num_data)] images_path_batch = images_path[ind] images_batch = read_image(images_path_batch) # print(images_batch.shape) # pdb.set_trace() feat_I = feats.eval(feed_dict={images: images_batch}) feat_all.append(feat_I) img_idxs.append(ind) feat_all = np.concatenate(feat_all, axis=0) img_idxs = np.concatenate(img_idxs, axis=0) return feat_all, img_idxs
def main(): # ================================================ # Load pre-trained model and remove higher level layers # ================================================ base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output) source_path = "db" source_paths = np.array( list( filter(lambda path: os.path.splitext(path)[1] in ['.jpg', '.jpeg'], np.array(os.listdir(source_path))))) # ================================================ # Read images and convert them to feature vectors # ================================================ imgs, filename_heads, X = [], [], [] for f in [sys.argv[1]]: # Process filename filename = os.path.splitext(f) # filename in directory head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue # Read image file img = image.load_img(f, target_size=(224, 224)) # load imgs.append(np.array(img)) # image filename_heads.append(head) # filename head # Pre-process for model input img = image.img_to_array(img) # convert to array img = np.expand_dims(img, axis=0) img = preprocess_input(img) features = model.predict(img).flatten() # features X.append(features) # append feature extractor X = np.array(X) # feature vectors imgs = np.array(imgs) # images # =========================== # Find k-nearest images to each image # =========================== n_neighbours = 3 pre_X = np.load('feature_matrix.npy') knn = kNN() # kNN model knn.compile(n_neighbors=n_neighbours, algorithm="brute", metric="cosine") knn.fit(pre_X) n_imgs = len(imgs) ypixels, xpixels = imgs[0].shape[0], imgs[0].shape[1] for ind_query in range(n_imgs): # Find top-k closest image feature vectors to each vector distances, indices = knn.predict(np.array([X[ind_query]])) distances = distances.flatten() indices = indices.flatten() indices, distances = find_topk_unique(indices, distances, n_neighbours) print(json.dumps(source_paths[indices].flatten().tolist())) print(distances)
def DeepOracle(lname='block5_conv1', fc_size=300, dropout=0.5): base = VGG19(weights='imagenet') layers = base.layers target_layer = base.get_layer(lname) target_idx = layers.index(target_layer) + 1 layers = layers[:target_idx] for l in layers: l.trainable = False in_shape = [int(e) for e in list(target_layer.output.shape[1:])] in_shape = tuple(in_shape) # Convolutional readout network readout = [ Convolution2D(2, (1, 1), activation='relu', padding='same', name='readout_conv1', input_shape=in_shape), BatchNormalization(name='readout_bn1'), Flatten(name='flatten'), Dense(fc_size, name='fc'), Dropout(dropout), Dense(37, name='predictions') ] layers.extend(readout) m = Sequential(layers) m.compile(optimizer='adam', loss='mse') return m
def extract_vgg19(): model = VGG19(weights='imagenet', include_top=False) print(model.summary()) X_dirname = '../../411a3/train' Y_filename = '../../411a3/train.csv' X_filelist = image.list_pictures(X_dirname) Y_list = np.loadtxt(Y_filename, dtype='str', delimiter=',')[1:] X_vgg = np.zeros((train_size, 512, 7, 7)) y_vgg = Y_list[:, 1].astype('int64').reshape(-1, 1) - 1 for i in range(train_size): img = image.load_img(X_filelist[i], target_size=target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) vgg19 = model.predict(x) X_vgg[i, :, :, :] = vgg19 print('Read image: ' + X_filelist[i]) # shuffle inputs and targets rnd_idx = np.arange(X_vgg.shape[0]) np.random.shuffle(rnd_idx) X_train = X_vgg[rnd_idx] y_train = y_vgg[rnd_idx] return X_train, y_train
def load_model (args): if args.model == 'inception': model = InceptionV3(include_top=True, weights='imagenet') preprocess_mode='tf' elif args.model == 'xception': model = Xception(include_top=True, weights='imagenet') preprocess_mode='tf' elif args.model == 'inceptionresnet': model = InceptionResNetV2(include_top=True, weights='imagenet') preprocess_mode='tf' elif args.model == 'mobilenet': model = MobileNet(include_top=True, weights='imagenet') preprocess_mode='tf' elif args.model == 'mobilenet2': model = MobileNetV2(include_top=True, weights='imagenet') preprocess_mode='tf' elif args.model == 'nasnet': model = NASNetLarge(include_top=True, weights='imagenet') preprocess_mode='tf' elif args.model == 'resnet': model = ResNet50(include_top=True, weights='imagenet') preprocess_mode='caffe' elif args.model == 'vgg16': model = VGG16(include_top=True, weights='imagenet') preprocess_mode='caffe' elif args.model == 'vgg19': model = VGG19(include_top=True, weights='imagenet') preprocess_mode='caffe' else: print ("Model not found") return model,preprocess_mode
def transfer(): global STYLE_FEATURES # print(content_image) input_img = tf.placeholder(tf.float32, [None, 224, 224, 3], "Input") style_img_pre = load_vgg19_image(STYLE_IMG) vgg19 = VGG19() vgg19.forward(input_img) #搭建前向图 if len(STYLE_FEATURES) != len(STYLE_LAYERS): #风格特征未提取 with tf.Session() as sess: # with tf.Graph.as_default(), tf.Graph.device('/Gpu:0'), tf.Session() as sess: sess.run(tf.global_variables_initializer()) # tf.summary.FileWriter('d:/mywork/vgg/log', sess.graph) #加载预训练数据 vgg19.load_pre_para(sess, without=['fc6', 'fc7', 'fc8']) #需要提取风格的特征层 conv1_1, conv2_1, conv3_1, conv4_1, conv5_1 = sess.run( [ vgg19.conv1_1, vgg19.conv2_1, vgg19.conv3_1, vgg19.conv4_1, vgg19.conv5_1 ], {input_img: style_img_pre}) #把风格特征加入全局变量,免得重复计算 for layer in STYLE_LAYERS: features = eval(layer) features = np.reshape(features, (-1, features.shape[3])) #把每一层的长*宽拉成一维 #求各层(特征)之间的两两相关性 gram矩阵可以看做feature之间的偏心协方差矩阵(即没有减去均值的协方差矩阵) gram = np.matmul(features.T, features) / features.size #不除? STYLE_FEATURES[layer] = gram
def main(): # print(content_image) input_img = tf.placeholder(tf.float32, [None, 224, 224, 3], "Input" ) vgg19 = VGG19() vgg19.forward(input_img) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) tf.summary.FileWriter('d:/mywork/vgg/log', sess.graph) vgg19.load_pre_para(sess, without=['fc6','fc7','fc8']) #加载预训练参数 可选择忽略的 ['fc7',fc8'...] # print_all_variable() for key in sorted(vgg19.data_dict.keys()): with tf.variable_scope('vgg19/'+key, reuse=True): # print(key, '参数:') # print('加载预训练参数后:') a = sess.run(tf.get_variable('weights')) # print(a[0]) # print('原来的预训练参数:') b = vgg19.data_dict[key][0] # print(b[0]) if not (a == b).all(): #判断矩阵是否相等 print(key + "没有加载预训练参数" ) img_in = tools.load_vgg19_image('../lib/unbr.jpg') _out = sess.run(vgg19.fc8, {input_img:img_in}) what_is(_out[0]) #分类结果
def VGG19_hieratt(query_in_size, query_embed_size, nb_classes): """Stack hierarchical attention on pre-trained VGG19. Requires https://github.com/fchollet/deep-learning-models""" base_model = VGG19(weights='imagenet') input_image = base_model.input input_question = Input(shape=(query_in_size, )) # question vector # Model up to 3rd block f_1 = Model(input=img_in, output=base_model.get_layer('block3_pool').output) f_1 = f_1(img_in) f_1 = Reshape((256, 28 * 28))(f_1) f_1 = Permute((2, 1))(f_1) q_1 = Dense(query_embed_size, activation='relu')(input_question) # Encode question # Add question embedding to each feature column q_1 = RepeatVector(28 * 28)(q_1) q_f = merge([f_1, q_1], 'concat') # Estimate and apply attention per feature att_1 = TimeDistributedDense(1, activation="sigmoid")(q_f) att_1 = Lambda(repeat_1, output_shape=(28 * 28, 256))(att_1) att_1 = merge([f_1, att_1], 'mul') # Reshape to the original feature map from previous layer att_1 = Permute((2, 1))(att_1) f_1_att = Reshape((256, 28, 28))(att_1) model = Model(input=[img_in, input_question], output=f_1_att) print model.summary()
def __init__(self, content, content_is_const, style, content_weight, style_weight, style_layer_weight_factor, total_variation_weight, print_loss, print_progress): self.content_is_const = content_is_const self.style = style self.net = VGG19(content.shape[2], content.shape[3]) self.net.load_params('vgg19_normalized.pkl') style_layer_names = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] style_layers = self.net.get_layers(style_layer_names) content_layer_names = ['conv4_2'] content_layers = self.net.get_layers(content_layer_names) all_layers = content_layers.copy() all_layers.update(style_layers) # expression for layer activations for content if self.content_is_const: self.content = content else: self.content = theano.shared(content) content_features = self.get_layer_activations_for_image(content_layer_names, content) # Pre-compute layer activations for style style_features = self.get_layer_activations_for_image(style_layer_names, self.style) # Get expressions for layer activations for generated image self.generated_image = theano.shared(floatX(np.zeros(shape=(1, 3, content.shape[2], content.shape[3])))) generated_features = {k: v for k, v in zip(all_layers.keys(), lasagne.layers.get_output(all_layers.values(), self.generated_image))} total_loss = loss.total_loss(content_features, style_features, generated_features, self.generated_image, content_layer_names, style_layer_names, content_weight, style_weight, style_layer_weight_factor, total_variation_weight, print_loss) grad = T.grad(total_loss, self.generated_image) # Theano function to evaluate loss and gradient self.f_outputs = theano.function([], [total_loss, grad]) self.print_progress = print_progress
def __init__(self, rnn_size, encoding_size, image_size=128, args=None): self.lstm = tf.contrib.rnn.BasicLSTMCell(rnn_size) self.args = args self.vgg19 = VGG19() self.vgg19.load_weights() self.encoding_size = encoding_size self.image_size = image_size
def select_architecture(architecture): if architecture == 'vgg16': return VGG16(weights='imagenet', include_top=False) elif architecture == 'vgg19': return VGG19(weights='imagenet', include_top=False) elif architecture == 'resnet50': return ResNet50(weights='imagenet', include_top=False)
def build(layers=None, target_scale=None): base_model = VGG19(weights='imagenet') print('extracting layers:') print(layers) f = h5py.File('../data/02activations.hdf5', 'r+') activations = [] for layer in layers: layer_activation = [] try: print('extracting ', layer, ' from cache...') layer_activation = f['activations/' + layer][:] except: print(layer, ' not in cache, rebuilding from source...') images = [ ki.img_to_array( ki.load_img('../data/images/%g.jpg' % id, target_size=(224, 224))) for id in np.arange(956) ] images = np.array(images) activation_fetcher = get_activation(base_model, layer) layer_activation = activation_fetcher.predict(images, batch_size=32, verbose=1) num_imgs = layer_activation.shape[0] num_features = layer_activation.shape[3] sc_fac = tuple( list( np.array([ num_imgs, target_scale[0], target_scale[1], num_features ]) / np.array(layer_activation.shape))) print('Rescaling by factor: ', sc_fac) print('resizing feature map...') layer_activation = zoom(layer_activation, sc_fac, order=0) # for img in tqdm(images): # img = np.expand_dims(img, axis=0) # layer_activation.extend([ feature ]) # layer_activation = np.concatenate(layer_activation, axis=0) print(layer_activation.shape) print('caching ', layer, '...') f.create_dataset('activations/' + layer, data=layer_activation) del images pass activations.extend([layer_activation]) f.close() del f, layer_activation gc.collect() activations = np.concatenate(activations, axis=3) return activations
def __init__(self, args, sess): op_base.__init__(self, args) self.sess = sess self.sess_arg = tf.Session() self.summaries = [] self.vgg = VGG19() self.model_path = os.path.join('gan_result', 'gan_model') self.code_path = os.path.join('gan_result', 'gan_encoder_code') self.eval_path = os.path.join('gan_result', 'gan_eval')
def __init__(self, x, is_training, batch_size): self.batch_size = batch_size self.vgg = VGG19(None, None, None) self.downscaled = self.downscale(x) self.imitation = self.generator(self.downscaled, is_training, False) self.real_output = self.discriminator(x, is_training, False) self.fake_output = self.discriminator(self.imitation, is_training, True) self.g_loss, self.d_loss = self.inference_losses( x, self.imitation, self.real_output, self.fake_output)
def Run(self, img_path, model_name): # config variables weights = 'imagenet' include_top = 0 train_path = 'jpg' classfier_file = 'output/flowers_17/' + model_name + '/classifier.cpickle' # create the pretrained models # check for pretrained weight usage or not # check for top layers to be included or not if model_name == "vgg16": from vgg16 import VGG16, preprocess_input base_model = VGG16(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "vgg19": from vgg19 import VGG19, preprocess_input base_model = VGG19(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": from resnet50 import ResNet50, preprocess_input base_model = ResNet50(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (224, 224) elif model_name == "inceptionv3": from inception_v3 import InceptionV3, preprocess_input base_model = InceptionV3(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('mixed9').output) image_size = (299, 299) elif model_name == "xception": from xception import Xception, preprocess_input base_model = Xception(weights=weights) model = Model(inputs=base_model.input, outputs=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None img = image.load_img(img_path, target_size=image_size) img_array = image.img_to_array(img) img_array = np.expand_dims(img_array, axis=0) img_array = preprocess_input(img_array) feature = model.predict(img_array) feature = feature.flatten() with open(classfier_file, 'rb') as f: model2 = pickle.load(f) pred = model2.predict(feature) prob = model2.predict_proba(np.atleast_2d(feature))[0] return pred, prob[0]
def load_all(): """ Load everything we need for generating """ print config.paths['decmodel'] # Skip-thoughts print 'Loading skip-thoughts...' stv = skipthoughts.load_model(config.paths['skmodels'], config.paths['sktables']) # Decoder print 'Loading decoder...' dec = decoder.load_model(config.paths['decmodel'], config.paths['dictionary']) # Image-sentence embedding print 'Loading image-sentence embedding...' vse = embedding.load_model(config.paths['vsemodel']) # VGG-19 print 'Loading and initializing ConvNet...' if config.FLAG_CPU_MODE: net = VGG19(weights='imagenet') else: net = build_convnet(config.paths['vgg']) # Captions print 'Loading captions...' cap = [] with open(config.paths['captions'], 'rb') as f: for line in f: cap.append(line.strip()) # Caption embeddings print 'Embedding captions...' cvec = embedding.encode_sentences(vse, cap, verbose=False) # Biases print 'Loading biases...' bneg = numpy.load(config.paths['negbias']) bpos = numpy.load(config.paths['posbias']) # Pack up z = {} z['stv'] = stv z['dec'] = dec z['vse'] = vse z['net'] = net z['cap'] = cap z['cvec'] = cvec z['bneg'] = bneg z['bpos'] = bpos return z
def __init__(self, sess, args): self.sess = sess self.model_name = "CPASSRnet" self.epoch_to_restore = args.epoch_to_restore self.is_training = (args.phase == 'train') self.gpu_num = len(args.visible_devices.split(',')) self.dataset_name = args.dataset_name self.vgg = VGG19() self.content_layers = ['relu1_1', 'pool1', 'conv2_2'] # cal content loss self.epoch = args.epoch self.decay_epoch = args.decay_epoch self.batch_size = args.batch_size self.sr_init_lr = args.sr_lr self.beta1 = args.beta1 self.img_h = args.img_h self.img_w = args.img_w self.img_ch = args.img_ch self.lambda_per = args.lambda_per self.lambda_recon = args.lambda_recon self.save_freq = args.save_freq self.sample_freq = args.sample_freq self.nc = args.nf self.num_downs = args.n_down self.sample_dir = check_folder( os.path.join(args.sample_dir, self.model_dir)) self.checkpoint_dir = check_folder( os.path.join(args.checkpoint_dir, self.model_dir)) self.test_dir = check_folder( os.path.join(args.test_dir, self.model_dir)) self.logs_dir = check_folder( os.path.join(args.logs_dir, self.model_dir)) self.train_left_paths = glob(os.path.join('./datasets', self.dataset_name, 'train_patches_%d_%d' % (self.img_h, self.img_w), '*L*.*')) self.train_right_paths = [ x.replace('L', 'R') for x in self.train_left_paths] self.train_pair_paths = list( zip(self.train_left_paths, self.train_right_paths)) self.dataset_num = len(self.train_pair_paths) self._build_model() show_all_variables()
def RetModel(): return [ VGG16(input_shape=(256, 256, 7), classes=2), VGG19(input_shape=(256, 256, 7), classes=2), ResNet50(input_shape=(256, 256, 7), classes=2), InceptionResNetV2(input_shape=(256, 256, 7), classes=2), DenseNet121(input_shape=(256, 256, 7), classes=2), DenseNet169(input_shape=(256, 256, 7), classes=2), DenseNet201(input_shape=(256, 256, 7), classes=2) ]
def RetModel()->Tuple[Model,Model,Model,Model,Model,Model,Model]: ''' To get all training model returns: load vgg,resnet and densenet model ''' return [VGG16(input_shape=(256,256,7),classes=2),VGG19(input_shape=(256,256,7),classes=2), ResNet50(input_shape=(256, 256, 7), classes=2),InceptionResNetV2(input_shape=(256, 256, 7), classes=2), DenseNet121(input_shape=(256, 256, 7),classes=2),DenseNet169(input_shape=(256, 256, 7), classes=2), DenseNet201(input_shape=(256, 256, 7),classes=2)]
def prepare_images(): print('Preparing images...') assert os.path.exists('data/train2014') assert os.path.exists('data/val2014') fnames_train2014 = os.listdir('data/train2014') fnames_val2014 = os.listdir('data/val2014') print('train2014 images:', len(fnames_train2014)) print('val2014 images:', len(fnames_val2014)) def get_id(fname): fname = fname.replace('COCO_train2014_', '') fname = fname.replace('COCO_val2014_', '') fname = fname.replace('.jpg', '') return int(fname) print('Loading VGG') from vgg19 import VGG19 model, block4 = VGG19(include_top=False) block4_features = K.function([model.input], [block4]) images_train2014 = np.memmap('data/images_train2014.mmap', dtype='float32', mode='write', shape=(len(fnames_train2014), 512, 196)) for i, fname in tqdm.tqdm(list(enumerate(fnames_train2014)), desc='train2014'): img = preprocessing.image.load_img('data/train2014/' + fname, target_size=(224, 224)) img = preprocessing.image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) img_features = block4_features([img])[0][0] images_train2014[i] = img_features.reshape([512, 196]) if (i % 1000) == 0: images_train2014.flush() images_train2014.flush() images_val2014 = np.memmap('data/images_val2014.mmap', dtype='float32', mode='write', shape=(len(fnames_val2014), 512, 196)) for i, fname in tqdm.tqdm(list(enumerate(fnames_val2014)), desc='val2014'): img = preprocessing.image.load_img('data/val2014/' + fname, target_size=(224, 224)) img = preprocessing.image.img_to_array(img) img = np.expand_dims(img, axis=0) img = preprocess_input(img) img_features = block4_features([img])[0][0] images_val2014[i] = img_features.reshape([512, 196]) if (i % 1000) == 0: images_val2014.flush() images_val2014.flush() pkl.dump(fnames_train2014, open('data/fnames_train2014.pkl', 'wb')) pkl.dump(fnames_val2014, open('data/fnames_val2014.pkl', 'wb'))
def train(): x = tf.placeholder(tf.float32, [None, 96, 96, 3]) t = tf.placeholder(tf.int32, [None]) is_training = tf.placeholder(tf.bool, []) model = VGG19(x, t, is_training) sess = tf.Session() with tf.variable_scope('vgg19'): global_step = tf.Variable(0, name='global_step', trainable=False) opt = tf.train.AdamOptimizer(learning_rate=learning_rate) train_op = opt.minimize(model.loss, global_step=global_step) init = tf.global_variables_initializer() sess.run(init) # Restore the latest model if tf.train.get_checkpoint_state('backup/'): saver = tf.train.Saver() saver.restore(sess, 'backup/latest') # Load the dataset x_train, t_train, x_test, t_test = load.load() # Train while True: epoch = int(sess.run(global_step) / np.ceil(len(x_train)/batch_size)) + 1 print('epoch:', epoch) perm = np.random.permutation(len(x_train)) x_train = x_train[perm] t_train = t_train[perm] sum_loss_value = 0 for i in tqdm(range(0, len(x_train), batch_size)): x_batch = augment.augment(x_train[i:i+batch_size]) t_batch = t_train[i:i+batch_size] _, loss_value = sess.run( [train_op, model.loss], feed_dict={x: x_batch, t: t_batch, is_training: True}) sum_loss_value += loss_value print('loss:', sum_loss_value) saver = tf.train.Saver() saver.save(sess, 'backup/latest', write_meta_graph=False) prediction = np.array([]) answer = np.array([]) for i in range(0, len(x_test), batch_size): x_batch = augment.augment(x_test[i:i+batch_size]) t_batch = t_test[i:i+batch_size] output = model.out.eval( feed_dict={x: x_batch, is_training: False}, session=sess) prediction = np.concatenate([prediction, np.argmax(output, 1)]) answer = np.concatenate([answer, t_batch]) correct_prediction = np.equal(prediction, answer) accuracy = np.mean(correct_prediction) print('accuracy:', accuracy)
def __init__(self, args, sess): op_base.__init__(self, args) self.sess = sess self.sess_arg = tf.Session() self.summaries = [] self.vgg = VGG19() self.model_path = os.path.join('imle_result', 'imle_model') self.eval_path = os.path.join('imle_result', 'imle_eval') if (not os.path.exists('imle_result')): os.mkdir('imle_result') os.mkdir('imle_result/imle_model') os.mkdir('imle_result/imle_eval')
def vgg_yolo(): vgg19 = VGG19(include_top=False, weights='imagenet', input_tensor=None, input_shape=(416, 416, 3)) ip = Input(shape=(416, 416, 3)) # Block1 h = vgg19.layers[1](ip) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) h = vgg19.layers[2](h) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) h = MaxPool2D(pool_size=(2, 2))(h) # Block2 for i in range(4, 6): h = vgg19.layers[i](h) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) h = MaxPool2D(pool_size=(2, 2))(h) # Block3 for i in range(7, 11): h = vgg19.layers[i](h) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) h = MaxPool2D(pool_size=(2, 2))(h) # Block4 for i in range(12, 16): h = vgg19.layers[i](h) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) h = MaxPool2D(pool_size=(2, 2))(h) # Block5 for i in range(17, 21): h = vgg19.layers[i](h) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) h = MaxPool2D(pool_size=(2, 2))(h) # Block6 for _ in range(0, 2): h = Conv2D(1024, (3, 3), strides=(1, 1), padding='same', use_bias=False)(h) h = BatchNormalization()(h) h = LeakyReLU(alpha=0.1)(h) # Block7 h = Conv2D(125, (1, 1), strides=(1, 1), kernel_initializer='he_normal')(h) h = Activation('linear')(h) h = Reshape((13, 13, 5, 25))(h) return Model(ip, h)
def vgg19_transfer_len(): base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) img_path = '1.jpg' 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) transferva = model.predict(x) nsamples, nx = transferva.shape d2_train_dataset = transferva.reshape((nsamples * nx)) K.clear_session() return d2_train_dataset.shape[0]
def __init__(self, x, is_training, batch_size): self.batch_size = batch_size self.vgg = VGG19(None, None, None) self.downscaled = self.downscale(x) self.bic_ref = tf.image.resize( self.downscaled, [self.image_size * 4, self.image_size * 4], method='bicubic') self.frame_sr, self.base_sr, self.imitation_sr = self.generator( self.downscaled, is_training, False) self.real_output = self.discriminator(x, is_training, False) self.fake_output = self.discriminator(self.base_sr, is_training, True) self.g_loss, self.d_loss = self.inference_losses( x, self.base_sr, self.imitation_sr, self.real_output, self.fake_output)
def __init__(self, x, x_com, is_training, batch_size): self.batch_size = batch_size self.vgg = VGG19(None, None, None) self.edge_hr = self.Laplacian(x) self.bic_ref = tf.image.resize_images( x_com, [self.image_size * 8, self.image_size * 8], method=2) self.cnn_edge, self.gan_edge, self.rnn_edge, self.att_edge, self.x_cnnmask, self.x_ganmask, self.x_rnnmask, self.CNN_sr, self.GAN_sr, self.RNN_sr, self.att_sr = self.generator( x_com, is_training, False) self.real_output = self.discriminator(x, is_training, False) self.fake_output = self.discriminator(self.GAN_sr, is_training, True) self.g_loss, self.d_loss = self.inference_losses( self.edge_hr, self.cnn_edge, self.gan_edge, self.rnn_edge, self.att_edge, x, self.CNN_sr, self.GAN_sr, self.RNN_sr, self.att_sr, self.real_output, self.fake_output)
def get_feature_map(self): dataprocess = DataProcess(self.args) image = self.read_image(self.args.style_image_path) image = tf.image.resize_images( image, [self.args.image_size, self.args.image_size], tf.image.ResizeMethod.NEAREST_NEIGHBOR) image = tf.expand_dims(image, 0) image = dataprocess.mean_image_subtraction(image) vgg = VGG19(self.args) vgg_net = vgg.feed_forward(image, reuse=True) with tf.Session() as sess: sess.run((tf.global_variables_initializer())) vgg_net_ = sess.run(vgg_net) return vgg_net_
def main(): # ================================================ # Load pre-trained model and remove higher level layers # ================================================ print("Loading VGG19 pre-trained model...") base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output) # ================================================ # Read images and convert them to feature vectors # ================================================ imgs, filename_heads, X = [], [], [] path = "db" print("Reading images from '{}' directory...\n".format(path)) for f in os.listdir(path): # Process filename filename = os.path.splitext(f) # filename in directory filename_full = os.path.join(path, f) # full path filename head, ext = filename[0], filename[1] if ext.lower() not in [".jpg", ".jpeg"]: continue print("Reading image: {}".format(filename)) # Read image file img = image.load_img(filename_full, target_size=(224, 224)) # load imgs.append(np.array(img)) # image filename_heads.append(head) # filename head # Pre-process for model input img = image.img_to_array(img) # convert to array img = np.expand_dims(img, axis=0) img = preprocess_input(img) features = model.predict(img).flatten() # features X.append(features) # append feature extractor X = np.array(X) # feature vectors imgs = np.array(imgs) # images print("imgs.shape = {}".format(imgs.shape)) print("X_features.shape = {}\n".format(X.shape)) np.save('feature_matrix.npy', X) print('generated feature matrix!')
def extract_vgg19_val(): model = VGG19(weights='imagenet', include_top=False) print(model.summary()) X_dirname = '../../411a3/val' X_filelist = image.list_pictures(X_dirname) X_vgg_val = np.zeros((val_size, 512, 7, 7)) for i in range(val_size): img = image.load_img(X_filelist[i], target_size=target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) vgg19 = model.predict(x) X_vgg_val[i, :, :, :] = vgg19 print('Read image: ' + X_filelist[i]) return X_vgg_val
def compute_features(net, im): """ Compute fc7 features for im """ if config.FLAG_CPU_MODE: from vgg19 import VGG19 #from imagenet_utils import preprocess_input, decode_predictions from keras.models import Model base_model = VGG19(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) pred_features = model.predict(im) return pred_features else: fc7 = numpy.array( lasagne.layers.get_output(net['fc7'], im, deterministic=True).eval()) return fc7