def vgg16_cifar10( path, # pylint: disable=invalid-name batch_norm=False, batch_size=128, num_threads=4, min_queue_examples=1000, mode="train"): """Cifar10 classification with a convolutional network.""" # Data. _maybe_download_cifar10(path) # pdb.set_trace() # Read images and labels from disk. if mode == "train": filenames = [ os.path.join(path, CIFAR10_FOLDER, "data_batch_{}.bin".format(i)) for i in xrange(1, 6) ] is_training = True elif mode == "test": filenames = [os.path.join(path, CIFAR10_FOLDER, "test_batch.bin")] is_training = False else: raise ValueError("Mode {} not recognised".format(mode)) depth = 3 height = 32 width = 32 label_bytes = 1 image_bytes = depth * height * width record_bytes = label_bytes + image_bytes reader = tf.FixedLengthRecordReader(record_bytes=record_bytes) _, record = reader.read(tf.train.string_input_producer(filenames)) record_bytes = tf.decode_raw(record, tf.uint8) label = tf.cast(tf.slice(record_bytes, [0], [label_bytes]), tf.int32) raw_image = tf.slice(record_bytes, [label_bytes], [image_bytes]) image = tf.cast(tf.reshape(raw_image, [depth, height, width]), tf.float32) # height x width x depth. image = tf.transpose(image, [1, 2, 0]) image = tf.math.divide(image, 255) queue = tf.RandomShuffleQueue( capacity=min_queue_examples + 3 * batch_size, min_after_dequeue=min_queue_examples, dtypes=[tf.float32, tf.int32], shapes=[image.get_shape(), label.get_shape()]) enqueue_ops = [queue.enqueue([image, label]) for _ in xrange(num_threads)] tf.train.add_queue_runner(tf.train.QueueRunner(queue, enqueue_ops)) vgg = VGG16(0.5, 10) def build(): image_batch, label_batch = queue.dequeue_many(batch_size) label_batch = tf.reshape(label_batch, [batch_size]) # pdb.set_trace() output = vgg._build_model(image_batch) # print(output.shape) return _xent_loss(output, label_batch) return build
# # while (~(frame is None)): # (inID, label) = self.predict(frame) # # def predict(self, frame): # image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32) # image = image.transpose((2, 0, 1)) # image = image.reshape((1,) + image.shape) # # image = preprocess_input(image) # preds = self.model.predict(image) # return decode_predictions(preds)[0] # ============================================================================= print('[info] loading network...') model = VGG16(weights='imagenet') cap = cv2.VideoCapture(0) if (cap.isOpened()): print("Camera OK") else: cap.open() # ============================================================================= # keras_thread = MyThread() # keras_thread.start() # ============================================================================= while (True): ret, original = cap.read() frame = cv2.resize(original, (224, 224))
def train(style_img_path, content_img_path, num_epochs, learning_rate, style_size, content_size, log_dir, style_loss_weights, content_loss_weights, reg_loss_weight, vgg_weights_path, ckpt_dir, log_iter=100, sample_iter=100, content_batch_size=4): iterator = tf.keras.preprocessing.image.DirectoryIterator datagen = tf.keras.preprocessing.image.ImageDataGenerator() content_iter = iterator(directory=content_img_path, batch_size=content_batch_size, \ target_size=(content_size, content_size), image_data_generator=datagen, shuffle=True, seed=2) style_iter = iterator(directory=style_img_path, batch_size=1, target_size=\ (style_size, style_size), image_data_generator=datagen, seed=2) total_iteration = num_epochs * content_iter.n // content_batch_size vgg_weights = np.load(vgg_weights_path) tf.reset_default_graph() config = tf.ConfigProto() config.gpu_options.allow_growth = True training_graph = tf.Graph() with training_graph.as_default() as g, tf.Session(config=config) as sess: s_placeholder = tf.placeholder(name='style', dtype=tf.float32, shape=[1, style_size, style_size, 3]) c_placeholder = tf.placeholder( name='content', dtype=tf.float32, shape=[content_batch_size, content_size, content_size, 3]) target_style_features = VGG16(s_placeholder, vgg_weights) target_content_features = VGG16(c_placeholder, vgg_weights) weights, biases = meta(target_style_features) transferred = transformer(c_placeholder, weights, biases) transferred_features = VGG16(transferred, vgg_weights) loss = loss_func(target_style_features, target_content_features, transferred_features, transferred, style_loss_weights, content_loss_weights, reg_loss_weight) optimizer = tf.train.AdamOptimizer( learning_rate=learning_rate).minimize(loss) loss_summary = tf.summary.scalar('loss', tf.squeeze(loss)) style_summary = tf.summary.image('style', s_placeholder) content_summary = tf.summary.image('content', c_placeholder) transferred_summary = tf.summary.image('transferred', transferred) image_summary = tf.summary.merge( [style_summary, content_summary, transferred_summary]) summary = tf.summary.FileWriter(graph=g, logdir=log_dir) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(var_list=tf.trainable_variables()) start = time.time() for epoch in range(num_epochs): i = 0 content_iter.reset() style_iter.reset() for c, _ in content_iter: if i + 1 == (content_iter.n // content_batch_size): break try: s, _ = style_iter.next() except StopIteration: style_iter.reset() s, _ = style_iter.next() _, w, b, cur_loss, cur_loss_summary, cur_image_summary \ = sess.run([optimizer, weights, biases, loss, loss_summary, image_summary], feed_dict={s_placeholder: s, c_placeholder: c}) if (i + 1) % log_iter == 0: print("Iteration: {0}, loss: {1}".format( epoch * content_iter.n // 4 + i + 1, cur_loss)) summary.add_summary(cur_loss_summary, epoch * content_iter.n // 4 + i + 1) if (i + 1) % sample_iter == 0: summary.add_summary(cur_image_summary, epoch * content_iter.n // 4 + i + 1) summary.flush() i += 1 save_path = os.path.join(ckpt_dir, 'ckpt') if not os.path.exists(ckpt_dir): os.makedirs(ckpt_dir, exist_ok=True) ckpt_path = saver.save(sess, save_path, write_meta_graph=False, global_step=epoch * content_iter.n // 4 + i + 1) print( "Checkpoint saved as: {ckpt_path}".format(ckpt_path=ckpt_path)) end = time.time() np.save('weights.npy', w) np.save('biases.npy', b) print("Finished {num_iters} iterations in {time} seconds.".format( num_iters=total_iteration, time=end - start))
#Shuffle the dataset x, y = shuffle(img_data, Y, random_state=2) # Split the dataset X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2) #################################################################################################################### #Training the feature extraction also image_input = Input(shape=(224, 224, 3)) model = VGG16(input_tensor=image_input, include_top=True, weights='imagenet') model.summary() last_layer = model.get_layer('block5_pool').output x = Flatten(name='flatten')(last_layer) x = Dense(128, activation='relu', name='fc1')(x) x = Dense(128, activation='relu', name='fc2')(x) out = Dense(num_classes, activation='softmax', name='output')(x) custom_vgg_model2 = Model(image_input, out) custom_vgg_model2.summary() # freeze all the layers except the dense layers for layer in custom_vgg_model2.layers[:-3]: layer.trainable = False
def main(args=None): K.tensorflow_backend.set_session(get_session()) # parse arguments if args is None: args = sys.argv[1:] args = parse_args(args) if args.model=='blurmapping': t_size=96 elif args.model=='vgg16': t_size=64 else: t_size=224 # data_path = args.data_dir # data_dir_list = os.listdir(data_path) img_data_list = [] if args.usepkldata==False: with open('./data/img_list.pkl', 'rb') as pk: img_list = _pickle.load(pk) for img in img_list: # img_path = data_path + '/' + dataset + '/' + img img_path = args.data_dir+'/'+img+'.jpg' img = image.load_img(img_path, target_size=(t_size,t_size)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) print('Input image shape:', x.shape) img_data_list.append(x) img_data = np.array(img_data_list) # img_data = img_data.astype('float32') print(img_data.shape) img_data = np.rollaxis(img_data, 1, 0) print(img_data.shape) img_data = img_data[0] print(img_data.shape) with open('./data/img_data'+str(t_size)+'.pkl', 'wb') as pk: _pickle.dump(img_data, pk) else: with open('./data/img_data'+str(t_size)+'.pkl', 'rb') as pk: img_data = _pickle.load(pk) print(img_data.shape) num_classes = 2 num_of_samples = img_data.shape[0] if args.object =='focus': with open('./data/focus.pkl', 'rb') as pk: labels = _pickle.load(pk) elif args.object == 'quality': with open('./data/quality.pkl', 'rb') as pk: labels = _pickle.load(pk) datagen = ImageDataGenerator( featurewise_center=True, featurewise_std_normalization=True, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True) datagen.fit(img_data) names = ['bad', 'good'] # convert class labels to on-hot encoding Y = np_utils.to_categorical(labels, num_classes) # Y = labels # encoder = LabelEncoder() # encoder.fit(Y) # encoded_Y = encoder.transform(Y) # Shuffle the dataset x, y = shuffle(img_data, Y, random_state=2) # Split the dataset X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2) ########################################################################################################################### if args.model=='resnet': # Training the classifier alone image_input = Input(shape=(224, 224, 3)) model = ResNet50(input_tensor=image_input, weights='imagenet') model.summary() last_layer = model.get_layer('avg_pool').output x = Flatten(name='flatten')(last_layer) x = Dense(1000, activation='relu', name='fc1')(x) x = Dropout(0.5)(x) out = Dense(num_classes, activation='softmax', name='output_layer')(x) custom_resnet_model = Model(inputs=image_input, outputs=out) custom_resnet_model.summary() for layer in custom_resnet_model.layers[:-1]: layer.trainable = False custom_resnet_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[f1_score,'accuracy']) t = time.time() filepath = "./data/resnet-"+str(args.object)+"-{epoch:02d}-{val_acc:.2f}.h5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] # hist = custom_resnet_model.fit(X_train, y_train, batch_size=32, epochs=args.epochs, verbose=1, # validation_data=(X_test, y_test), # callbacks=callbacks_list) Y = np_utils.to_categorical(labels, num_classes) hist = custom_resnet_model.fit_generator(datagen.flow(img_data, Y, batch_size=32),callbacks=callbacks_list, steps_per_epoch=len(img_data) / 32, epochs=1000) print('Training time: %s' % (t - time.time())) (loss, accuracy) = custom_resnet_model.evaluate(X_test, y_test, batch_size=args.batch_size, verbose=1) print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100)) ######################################################################################### elif args.model=='vgg16': # Custom_vgg_model_1 # Training the classifier alone # image_input = Input(shape=(224, 224, 3)) Y = labels encoder = LabelEncoder() encoder.fit(Y) encoded_Y = encoder.transform(Y) # Shuffle the dataset x, y = shuffle(img_data, encoded_Y, random_state=2) # Split the dataset X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2) vgg_model = VGG16( include_top=False, weights='imagenet') for layer in vgg_model.layers[:-1]: layer.trainable = False inp = Input(shape=(64, 64, 3), name='input_image') output_vgg_conv = vgg_model(inp) x_1 = Flatten(name='flatten')(output_vgg_conv) x_1 = Dense(512, activation='relu', name='fc1')(x_1) x_1 = Dropout(0.5)(x_1) x_1 = Dense(128, activation='relu', name='fc2')(x_1) x_1 = Dropout(0.25)(x_1) x_1 = Dense(8, activation='relu', name='fc3')(x_1) x_1 = Dropout(0.125)(x_1) x_1 = Dense(1, activation='sigmoid', name='frontalpred')(x_1) custom_vgg_model = Model(input=inp, output=x_1) custom_vgg_model.summary() filepath = "./data/vgg16-"+str(args.object)+"-{epoch:02d}-{val_acc:.2f}.h5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] custom_vgg_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=[f1_score,'accuracy']) t = time.time() # t = now() hist = custom_vgg_model.fit(X_train, y_train, batch_size=32, epochs=args.epochs, verbose=1, validation_data=(X_test, y_test), callbacks=callbacks_list) print('Training time: %s' % (t - time.time())) (loss, accuracy) = custom_vgg_model.evaluate(X_test, y_test, batch_size=args.batch_size, verbose=1) print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100)) elif args.model=='blurmapping': model = KitModel(weight_file='blurMapping.npy') model.summary() last_layer = model.get_layer('relu5_3').output x = Flatten(name='flatten')(last_layer) x = Dense(1024, activation='relu', name='fc_1')(x) x = Dropout(0.5)(x) x = Dense(128, activation='relu', name='fc2')(x) x = Dropout(0.25)(x) out = Dense(num_classes, activation='softmax', name='output_layer')(x) custom_model = Model(inputs=model.input, outputs=out) custom_model.summary() for layer in custom_model.layers[:-1]: layer.trainable = False filepath = "./data/blurmapping-"+str(args.object)+"-{epoch:02d}-{val_acc:.2f}.h5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] custom_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[f1_score,'accuracy']) t = time.time() # t = now() # hist = custom_model.fit(X_train, y_train, batch_size=32, epochs=args.epochs, verbose=1, # validation_data=(X_test, y_test), # callbacks=callbacks_list) Y = np_utils.to_categorical(labels, num_classes) hist = custom_model.fit_generator(datagen.flow(img_data, Y, batch_size=args.batch_size),callbacks=callbacks_list, steps_per_epoch=1000, epochs=50) print('Training time: %s' % (t - time.time()))
shuffle=True) valid = gen.flow_from_directory( VALID_PATH, target_size=(img_size, img_size), batch_size=batch_size, class_mode="categorical", shuffle=False) pseudo = gen.flow_from_directory( TEST_PATH, target_size=(img_size, img_size), batch_size=batch_size, classes=["c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9"], class_mode="categorical", shuffle=False) vgg = VGG16(dropout) vgg.finetune_dense(10) vgg.model.compile( optimizer=Adam(lr=lr, decay=decay), loss="categorical_crossentropy", metrics=["accuracy"]) vgg.model.load_weights("data/weights.h5") pseudo.__dict__["classes"] = np.argmax(vgg.model.predict_generator(pseudo, val_samples=pseudo.nb_sample), axis=-1) for i in range(epochs): vgg.model.fit_generator( train, samples_per_epoch=train.nb_sample,
## print: [[u'n02504458', u'African_elephant']] # #%% model = ResNet50(include_top=False, weights='imagenet') model.summary() model.layers[-1].get_config() img_path = 'elephant.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) preds = model.predict(x) print(preds) #%% model = VGG16(include_top=True, weights='imagenet') img_path = 'elephant.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) print('Input image shape:', x.shape) preds = model.predict(x) print('Predicted:', decode_predictions(preds)) model.summary() model.layers[-1].get_config() #%%
import cv2 from keras import datasets from vgg16 import VGG16 import gzip start_time = time.time() # to run model evluation on 1 core import os os.environ['CUDA_VISIBLE_DEVICES'] = '1' #transfer learning ishape = 100 model_vgg = VGG16(include_top=False, weights='imagenet', input_shape=(ishape, ishape, 3)) model = Model(inputs=model_vgg.input, outputs=model_vgg.get_layer('block4_pool').output) # load data data_dir = "../Fashion_mnist/" def extract_data(filename, num_data, head_size, data_size): with gzip.open(filename) as bytestream: bytestream.read(head_size) buf = bytestream.read(data_size * num_data) data = np.frombuffer(buf, dtype=np.uint8).astype(np.float) return data
model_name = args.model if model_name == 'caffenet': model_name_capital = 'CaffeNet' else: model_name_capital = model_name.upper() param_dir = 'fast-rcnn/data/fast_rcnn_models' param_fn = '%s/%s_fast_rcnn_iter_40000.caffemodel' % (param_dir, model_name) model_dir = 'fast-rcnn/models/%s' % model_name_capital model_fn = '%s/test.prototxt' % model_dir if model_name == 'caffenet': model = CaffeNet() elif model_name == 'vgg16': model = VGG16() elif model_name == 'vgg_cnn_m_1024': model = VGG_CNN_M_1024() else: raise ValueError('Unsupported model name: %s' % model_name) net = caffe.Net(model_fn, param_fn, caffe.TEST) for name, param in net.params.iteritems(): layer = getattr(model, name) print name, param[0].data.shape, param[1].data.shape, print layer.W.data.shape, layer.b.data.shape assert layer.W.data.shape == param[0].data.shape layer.W.data = param[0].data
DATA_DIR = 'data/' IMG_WIDTH, IMG_HEIGHT = 224, 224 SIZE = IMG_WIDTH, IMG_HEIGHT # Resize images. for root, dirs, files in os.walk(DATA_DIR): for filename in files: filepath = os.path.join(root, filename) if filepath.endswith(".JPG") or filepath.endswith(".JPEG"): try: img = Image.open(filepath) img.thumbnail(SIZE, Image.ANTIALIAS) img.save(filepath, "JPEG") print('Resized: %s' % filepath) except IOError: print('Could not resize: %s' % filepath) batch_size = 8 vgg = VGG16() batches = vgg16.get_batches(vgg, os.path.join(DATA_DIR, 'train'), batch_size=batch_size) val_batches = vgg16.get_batches(vgg, os.path.join(DATA_DIR, 'valid'), batch_size=batch_size) finetuned_model = vgg16.finetune(vgg, batches) finetuned_model.fit_generator(batches, steps_per_epoch=64, epochs=1, validation_data=val_batches, validation_steps=16)
from keras.models import Model from keras import optimizers from keras import initializers from base_model import BaseModel from vgg16 import VGG16 from resnet164 import ResNet164 #from mobilenet import MobileNet from wide_resnet_28_10 import WideResNet28_10 import numpy as np import argparse import utils import os MODEL_NAME = 'SuperLearner' # This should be modified when the model name changes. PATH = './models/' models = [VGG16(), ResNet164(), WideResNet28_10()]#, MobileNet()] class SuperLearner(BaseModel): ''' 1. Conv1D (N, 10, NumOfModelsToBeEnsembled) 2. Softmax '_build()' is only modified when the model changes. HowToUse: model = SuperLearner(models_to_be_ensembled) * all funtionalities are written in BaseModel.py ''' def __init__(self, models): self.models = self._remove_softmax_from(models) # Don't use test data information for training
def get_cell_idx(lon, lat, top_left_x_coords, top_left_y_coords): lon_idx = np.where(top_left_x_coords < lon)[0][-1] lat_idx = np.where(top_left_y_coords > lat)[0][-1] return lon_idx, lat_idx npzfile = np.load('intermediate_files/nightlight.npz') print npzfile.files top_left_x_coords = npzfile['top_left_x_coords'] top_left_y_coords = npzfile['top_left_y_coords'] bands_data = npzfile['bands_data'] # get image featuers base_model = VGG16(weights='imagenet') model = Model(input=base_model.input, output=base_model.get_layer('fc2').output) def get_input_feature(img_path): img = image.load_img(img_path, target_size=(224, 224)) # img = image.load_img(img_path) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = model.predict(x) return features[0] def get_daytime_feature(sample):
import sys import argparse from vgg16 import VGG16 import numpy as np from keras.preprocessing import image from imagenet_utils import preprocess_input,decode_predictions model = VGG16(weights='imagenet', include_top=True, input_tensor=None, input_shape=None) a = argparse.ArgumentParser() a.add_argument("--image", help="path to image") args = a.parse_args() if args.image is None: a.print_help() sys.exit(1) if args.image is not None: img_path = args.image # img_path = 'elephant.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) print('Input image shape:', x.shape) preds = model.predict(x) print('Predicted:', decode_predictions(preds))
parser.add_argument('--epochs-ft', type=int, default=50, help='finetune number of epochs, default=50') parser.add_argument('--lr-ft', type=float, default=0.001, help='finetune initial learning rate, default=0.001') parser.add_argument('--reg-ft', type=float, default=5e-6, help='finetune reg strength, default=5e-6') args = parser.parse_args() assert ((not args.skip_pt) or (args.path != '')) device = 'cuda' if torch.cuda.is_available() else 'cpu' # --------------------------------------- # # --- Full precision model load/train --- # # --------------------------------------- # if args.model == "vgg16": net = VGG16() elif args.model == "resnet50": net = ResNet50() else: print("Model {} not supported!".format(args.model)) sys.exit(0) net = net.to(device) # Uncomment to load pretrained weights #net.load_state_dict(torch.load("net_before_pruning.pt")) # Comment if you have loaded pretrained weights # Tune the hyperparameters here. if not args.skip_pt: train(net, epochs=args.epochs, batch_size=args.batch, lr=args.lr, reg=args.reg, checkpoint_path=args.ckpt_dir) else:
def vqa_mlp(batch_size=32, epochs=4, max_len=10): vgg = VGG16(include_top=True, weights='imagenet') gen_vocab() embeddings = load_embeddings() vocab_size = get_vocab_size() model = modelQA(vocab_size + 1, 4096, 100, max_len, embeddings) ques_train, ans_train, img_train, ques_val, ans_val, img_val = parse_QA() pdb.set_trace() # Parse all training images and load them into memory for epoch in xrange(epochs): Img_feats = [] ques_feats = [] labels = [] # Indices over training data train_ind = range(len(ques_train)) random.shuffle(train_ind) batch_ind = 0 n = len(ques_train) # iterate over training exampled in shuffled indices order for i in xrange(0, (n / batch_size) + 1): batch_index = train_ind[batch_ind * batch_size:(batch_ind + 1) * batch_size] img_batch = img_train[batch_ind * batch_size:(batch_ind + 1) * batch_size] ques_batch = ques_train[batch_ind * batch_size:(batch_ind + 1) * batch_size] ans_batch = ans_train[batch_ind * batch_size:(batch_ind + 1) * batch_size] for img in img_batch: img_path = 'COCO_' + 'train2014' + '_' + str(img).zfill( 12) + '.jpg' img_feat = img_feats(IMG_DIR + 'train2014/' + img_path) Img_feats.append(img_feat) for ques in ques_batch: ques_feat = encode_text(ques) ques_feats.append(ques_feat) for ans in ans_batch: ans_feat = encode_ans(ans) labels.append(ans_feat) ques_feats = np.array(ques_feats) ques_feats = pad_sequences(ques_feats, max_len) Img_feats = np.array(Img_feats) Img_feats = Img_feats.reshape(len(Img_feats), 4096) #pdb.set_trace() loss, acc = model.train_on_batch([Img_feats, ques_feats], labels) print("training Loss for epoch %d is %f with acc. %f" % (epoch, loss, acc)) Img_feats = [] ques_feats = [] labels = [] #pdb.set_trace() batch_ind += 1 val_loss = 0 val_acc = 0 Img_feats = [] ques_feats = [] labels = [] for img in img_val: img_path = 'COCO_' + 'val2014' + '_' + str(img).zfill(12) + '.jpg' img_feat = img_feats(IMG_DIR + 'val2014/' + img_path) Img_feats.append(img_feat) for ques in ques_val: ques_feat = encode_text(ques) ques_feats.append(ques_feat) for ans in ans_val: ans_feat = encode_ans(ans) labels.append(ans_feat) ques_feats = np.array(ques_feats) ques_feats = pad_sequences(ques_feats, max_len) Img_feats = np.array(Img_feats) Img_feats = Img_feats.reshape(len(Img_feats), 4096) loss, acc = model.test_on_batch([Img_feats, ques_feats], labels) print("VALIDATIOn Loss for epoch %d is %f with acc. %f" % (epoch, loss, acc))
from vgg16 import VGG16 from vgg16 import CIFAR10Loader import sys if len(sys.argv) != 2: print("Error!") sys.exit() job = sys.argv[1] # Load the dataset CIFAR10_data_path = '/media/salman/DATA/General Datasets/cifar-10-batches-py/' cifarLoader = CIFAR10Loader(CIFAR10_data_path) # Load the network network = VGG16(cifarLoader) if job == 'train': network.train() elif job == 'test': network.test() network.sess.close()
img_path = 'img_dr-003.jpeg' 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) print('Input image shape:', x.shape) preds = model.predict(x) print('Predicted:', decode_predictions(preds)) model.summary() model.layers[-1].get_config() #%% model = VGG16(weights='imagenet', include_top=False) model.summary() model.layers[-1].get_config() img_path = 'img_dr-003.jpeg' 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) #%% model = vgg16(include_top=True,weights='imagenet')
def model_gen(): model = VGG16(weights='imagenet', include_top=True, input_shape = (224, 224, 3)) return model
model.add(Activation('relu')) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes)) model.add(Activation('softmax')) model.summary() ''' #model = deep_rank_model() model = VGG16(input_shape=input_shape) bind_model(model) if config.pause: nsml.paused(scope=locals()) bTrainmode = False if config.mode == 'train': bTrainmode = True """ Initiate RMSprop optimizer """ opt = keras.optimizers.rmsprop(lr=0.00045, decay=1e-6) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) """ Load data """ print('dataset path', DATASET_PATH)
def train(FLAG): print("Reading dataset...") if FLAG.dataset == 'CIFAR-10': train_data = CIFAR10(train=True) test_data = CIFAR10(train=False) elif FLAG.dataset == 'CIFAR-100': train_data = CIFAR100(train=True) test_data = CIFAR100(train=False) else: raise ValueError("dataset should be either CIFAR-10 or CIFAR-100.") Xtrain, Ytrain = train_data.train_data, train_data.train_labels Xtest, Ytest = test_data.test_data, test_data.test_labels print("Build VGG16 models...") vgg16 = VGG16(FLAG.init_from, prof_type=FLAG.prof_type) # build model using dp dp = [(i + 1) * 0.05 for i in range(1, 20)] vgg16.build(dp=dp) # define tasks tasks = ['100', '50'] print(tasks) saver = tf.train.Saver(tf.global_variables(), max_to_keep=len(tasks)) checkpoint_path = os.path.join(FLAG.save_dir, 'model.ckpt') tvars_trainable = tf.trainable_variables() for rm in vgg16.gamma_var: tvars_trainable.remove(rm) print('%s is not trainable.' % rm) # useful function def initialize_uninitialized(sess): global_vars = tf.global_variables() is_not_initialized = sess.run( [tf.is_variable_initialized(var) for var in global_vars]) not_initialized_vars = [ v for (v, f) in zip(global_vars, is_not_initialized) if not f ] if len(not_initialized_vars): sess.run(tf.variables_initializer(not_initialized_vars)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # hyper parameters learning_rate = 2e-4 batch_size = 32 alpha = 0.5 early_stop_patience = 4 min_delta = 0.0001 # optimizer # opt = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9) opt = tf.train.AdamOptimizer(learning_rate=learning_rate) # recorder epoch_counter = 0 # tensorboard writer writer = tf.summary.FileWriter(FLAG.log_dir, sess.graph) # progress bar ptrain = IntProgress() pval = IntProgress() display(ptrain) display(pval) ptrain.max = int(Xtrain.shape[0] / batch_size) pval.max = int(Xtest.shape[0] / batch_size) # initial task obj = vgg16.loss_dict[tasks[0]] while (len(tasks)): # acquire a new task cur_task = tasks[0] tasks = tasks[1:] new_obj = vgg16.loss_dict[cur_task] # just finished a task if epoch_counter > 0: # save models saver.save(sess, checkpoint_path, global_step=epoch_counter) # task-wise loss aggregation # obj = tf.add(tf.multiply(1-alpha,obj), tf.multiply(alpha,new_obj)) obj = tf.add(obj, new_obj) # optimizer train_op = opt.minimize(obj, var_list=tvars_trainable) # re-initialize initialize_uninitialized(sess) # reset due to adding a new task patience_counter = 0 current_best_val_loss = 100000 # a large number # optimize when the aggregated obj while (patience_counter < early_stop_patience): stime = time.time() bar_train = Bar( 'Training', max=int(Xtrain.shape[0] / batch_size), suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds') bar_val = Bar( 'Validation', max=int(Xtest.shape[0] / batch_size), suffix='%(index)d/%(max)d - %(percent).1f%% - %(eta)ds') # training an epoch for i in range(int(Xtrain.shape[0] / batch_size)): st = i * batch_size ed = (i + 1) * batch_size sess.run([train_op], feed_dict={ vgg16.x: Xtrain[st:ed, :, :, :], vgg16.y: Ytrain[st:ed, :] }) ptrain.value += 1 ptrain.description = "Training %s/%s" % (i, ptrain.max) bar_train.next() # validation val_loss = 0 val_accu = 0 for i in range(int(Xtest.shape[0] / 200)): st = i * 200 ed = (i + 1) * 200 loss, accu, epoch_summary = sess.run( [obj, vgg16.accu_dict[cur_task], vgg16.summary_op], feed_dict={ vgg16.x: Xtest[st:ed, :], vgg16.y: Ytest[st:ed, :] }) val_loss += loss val_accu += accu pval.value += 1 pval.description = "Testing %s/%s" % (i, pval.value) val_loss = val_loss / pval.value val_accu = val_accu / pval.value # early stopping check if (current_best_val_loss - val_loss) > min_delta: current_best_val_loss = val_loss patience_counter = 0 else: patience_counter += 1 # shuffle Xtrain and Ytrain in the next epoch idx = np.random.permutation(Xtrain.shape[0]) Xtrain, Ytrain = Xtrain[idx, :, :, :], Ytrain[idx, :] # epoch end writer.add_summary(epoch_summary, epoch_counter) epoch_counter += 1 ptrain.value = 0 pval.value = 0 bar_train.finish() bar_val.finish() print( "Epoch %s (%s), %s sec >> obj loss: %.4f, task at %s: %.4f" % (epoch_counter, patience_counter, round(time.time() - stime, 2), val_loss, cur_task, val_accu)) saver.save(sess, checkpoint_path, global_step=epoch_counter) writer.close()
def load_encoding_model(): model = VGG16(weights='imagenet', include_top=True, input_shape=(224, 224, 3)) return model
# dimensions for the network -- then convert the PIL image to a # NumPy array print("[INFO] loading and preprocessing image...") image = image_utils.load_img(fullPathImg, target_size=(224, 224)) image = image_utils.img_to_array(image) # our image is now represented by a NumPy array of shape (3, 224, 224), # but we need to expand the dimensions to be (1, 3, 224, 224) 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 image = np.expand_dims(image, axis=0) image = preprocess_input(image) # load the VGG16 network print("[INFO] loading network...") model = VGG16(weights="imagenet") # classify the image print("[INFO] classifying image...") preds = model.predict(image) # (inID, label) = decode_predictions(preds)[0] P = decode_predictions(preds) (imagenetID, label, prob) = P[0][0] # display the predictions to our screen print("ImageNet ID: {}, Label: {}".format(imagenetID, label)) imgAndClass = [] imgAndClass.append(img) imgAndClass.append(label) resultList.append(imgAndClass)
from sklearn.preprocessing import LabelEncoder #from keras.applications import VGG16 from vgg16 import VGG16 from keras.applications import imagenet_utils from keras.preprocessing.image import img_to_array from keras.preprocessing.image import load_img from pyimagesearch import config from imutils import paths import numpy as np import pickle import random import os # load the VGG16 network and initialize the label encoder print("[INFO] loading network...") model = VGG16(weights="imagenet", include_top=False) le = None # loop over the data splits for split in [config.TRAIN, config.TEST, config.VAL]: # grab all image paths in the current split print("[INFO] processing '{} split'...".format(split)) p = os.path.sep.join([config.BASE_PATH, split]) imagePaths = list(paths.list_images(p)) # # randomly shuffle the image paths and then extract the class # labels from the file paths (labels is the artist) random.shuffle(imagePaths) labels = [p.split(os.path.sep)[-2] for p in imagePaths] # if the label encoder is None, create it
def initialize_vgg16(self): self.vgg16 = VGG16(input_shape=self.img_shape, trainable=False, session=self.session)