z.append(np.array(img)) pred_z= modelx.predict(np.array(z)) pred_digits_z=np.argmax(pred,axis=1) preds_z=np.round(pred_z,0) categorical_preds_z = pd.DataFrame(preds_z).idxmax(axis=1) ################################################################################################ # the vgg16 model from keras.applications.vgg16 import VGG16 from keras.models import Model from keras.layers import Dense from keras.layers import Flatten # load model without classifier layers model = VGG16(include_top=False, input_shape=(224, 224, 3)) # add new classifier layers flat1 = Flatten()(model.outputs) class1 = Dense(1024, activation='relu')(flat1) output = Dense(2, activation='sigmoid')(class1) # define new model model = Model(inputs=model.inputs, outputs=output) # summarize model.summary()
import pandas as pd from PIL import Image from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions from keras.preprocessing.image import img_to_array, load_img import tensorflow as tf #wierd configs config = tf.ConfigProto() config.gpu_options.allow_growth = True tf.keras.backend.set_session(tf.Session(config=config)) PATH = "/home/ok/OAI/Bunnys" #get folders folders = [f for f in listdir(PATH) if isdir(join(PATH, f))] model = VGG16() bunny_pictures = [] non_bunny = [] for folder in folders: folder_path = join(PATH, folder) #non bunny data in the folder non_bunny_pics = [join(folder_path, f) for f in listdir(folder_path) if isfile(join(folder_path, f))] # for pic in non_bunny_pics: # image = Image.open(pic) # image = image.resize((224,224)) # print(image.format) # print(image.mode) # print(image.size)
x.append(img) y+=[classes.index(fol) for i in range(len(files))] x = np.array(x) y = np.array(y) print len(y) x,y = shuffle(x,y) y=MultiLabelBinarizer().fit_transform(y.reshape(-1, 1)) print x.shape from keras.applications.vgg16 import VGG16 from keras.preprocessing import image from keras.applications.vgg16 import preprocess_input import numpy as np model = VGG16(weights='imagenet', include_top=False) print "Model Loaded......" features = model.predict(x) features_test = model.predict(x_test) model = Sequential() model.add(Flatten(input_shape=features.shape[1:])) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4, activation='softmax')) sgd = SGD(lr=0.00005, decay = 1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy']) callbacks = [ EarlyStopping(monitor='val_loss', patience=10, verbose=0), ModelCheckpoint('augment_1_1024.h5', monitor='val_loss', save_best_only=True, verbose=0) ] model.load_weights('augment_1_1024.h5') pred = model.predict(features_test) print pred
# img = image.img_to_array(img) # x_test.append(img) #x_train = np.array(x_train, np.float16)/255. #y_train = np.array(y_train) #x_test = np.array(x_test, np.float16)/255. # split training data x_train, x_valid, y_train, y_valid = train_test_split(x_train, y_train, test_size=0.3, random_state=123) ''' img_size = 50 # pretrained model #base_model = ResNet50(weights='imagenet', include_top=False, input_shape=[img_size, img_size,3]) base_model = VGG16(weights='imagenet', include_top=False, input_shape=[img_size, img_size, 3]) # train only the top layers (which were randomly initialized) for layer in base_model.layers: layer.trainable = False # add new layers on top x = base_model.output x = Flatten()(x) #x = Dense(1024, activation='relu')(x) x = Dense(512, activation='relu')(x) predictions = Dense(120, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) learning_rate = 1e-3 adam = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0)
# -*- coding: utf-8 -*- """ Created on Mon Sep 7 17:48:55 2020 @author: lenovo """ from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense from keras.preprocessing.image import ImageDataGenerator import numpy as np from keras.applications.vgg16 import VGG16 import keras model = VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3)) print('load model ok') datagen = ImageDataGenerator(rescale=1. / 255) train_generator = datagen.flow_from_directory( r'C:\Users\lenovo\Desktop\Keras\train', target_size=(150, 150), batch_size=20, class_mode=None, shuffle=False) test_generator = datagen.flow_from_directory( r'C:\Users\lenovo\Desktop\Keras\test', target_size=(150, 150), batch_size=20,
#from keras.applications.resnet50 import ResNet50 from keras.applications.vgg16 import VGG16 from keras.applications.vgg16 import preprocess_input from keras.preprocessing import image from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential import numpy as np from glob import glob import matplotlib.pyplot as plt IMAGE_SIZE = [224, 224] # Import the Vgg 16 library as shown below and add preprocessing layer to the front of VGG # Here we will be using imagenet weights vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False) # don't train existing weights for layer in vgg.layers: layer.trainable = False # useful for getting number of output classes folders = glob('CovidDataset1/Train/*') # our layers - you can add more if you want x = Flatten()(vgg.output) prediction = Dense(len(folders), activation='softmax')(x) # create a model object
features_path = config["features_path"] labels_path = config["labels_path"] test_size = config["test_size"] results = config["results"] model_path = config["model_path"] seed = config["seed"] classifier_path = config["classifier_path"] # load the trained classifier / Eğitimli sınıflandırıcısının yüklenmesi print("[INFO] loading the classifier...") classifier = pickle.load(open(classifier_path, 'rb')) # pretrained models needed to perform feature extraction on test data too #Test verileri üzerinde de özellik çıkarımı gerçekleştirmek için önceden eğitilmiş modellerin kullanımı if model_name == "vgg16": base_model = VGG16(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "vgg19": base_model = VGG19(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None
#import VGG16 network model and other necessary libraries from keras.applications.vgg16 import VGG16 from keras.preprocessing import image from keras.applications.vgg16 import preprocess_input import numpy as np #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)
if len(sys.argv) > 1: MAX_STEP = int(sys.argv[1]) if len(sys.argv) > 2: CAPACITY = int(sys.argv[2]) #学习速率 learning_rate = 0.0001 #目录 train_dir = "./image/" log_dir = "./VGG_log/VGG_model_new_" + str(MAX_STEP) + ".h5" #模型构建 model_vgg = VGG16(include_top=False, weights='imagenet', input_shape=(IMG_H, IMG_W, 3)) for layer in model_vgg.layers: layer.trainable = False model = Flatten()(model_vgg.output) model = Dense(1024, activation='relu', name='fc1')(model) model = Dense(1024, activation='relu', name='fc2')(model) model = Dropout(0.5)(model) model = Dense(10, activation='softmax', name='prediction')(model) model_vgg_train = Model(inputs=model_vgg.input, outputs=model, name='vgg16') model_vgg_train.summary() #设定损失函数 sgd = SGD(lr=learning_rate, decay=1e-5) model_vgg_train.compile(loss='categorical_crossentropy',
from keras.applications.vgg16 import VGG16, preprocess_input from keras.preprocessing.image import load_img, img_to_array from keras.layers import Input vgg16_encoder = VGG16(include_top=False, input_tensor=Input(shape=(224, 224, 3)), pooling="avg") def process_image(image): """ Map image to VGG16 input shape. """ image = img_to_array(load_img(image, target_size=(224, 224))) return preprocess_input( image.reshape(1, image.shape[0], image.shape[1], image.shape[2])) def encode(image): return vgg16_encoder.predict(process_image(image), verbose=0) if __name__ == '__main__': model = vgg16_encoder()
def get_vgg_7conv(input_shape): img_input = Input(input_shape) vgg16_base = VGG16(input_tensor=img_input, include_top=False) for l in vgg16_base.layers: l.trainable = True conv1 = vgg16_base.get_layer("block1_conv2").output conv2 = vgg16_base.get_layer("block2_conv2").output conv3 = vgg16_base.get_layer("block3_conv3").output pool3 = vgg16_base.get_layer("block3_pool").output conv4 = Conv2D(384, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block4_conv1")(pool3) conv4 = Conv2D(384, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block4_conv2")(conv4) pool4 = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(conv4) conv5 = Conv2D(512, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block5_conv1")(pool4) conv5 = Conv2D(512, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block5_conv2")(conv5) pool5 = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(conv5) conv6 = Conv2D(512, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block6_conv1")(pool5) conv6 = Conv2D(512, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block6_conv2")(conv6) pool6 = MaxPooling2D((2, 2), strides=(2, 2), name='block6_pool')(conv6) conv7 = Conv2D(512, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block7_conv1")(pool6) conv7 = Conv2D(512, (3, 3), activation="relu", padding='same', kernel_initializer="he_normal", name="block7_conv2")(conv7) up8 = concatenate([ Conv2DTranspose(384, (3, 3), activation="relu", kernel_initializer="he_normal", strides=(2, 2), padding='same')(conv7), conv6 ], axis=3) conv8 = Conv2D(384, (3, 3), activation="relu", kernel_initializer="he_normal", padding='same')(up8) up9 = concatenate([ Conv2DTranspose(256, (3, 3), activation="relu", kernel_initializer="he_normal", strides=(2, 2), padding='same')(conv8), conv5 ], axis=3) conv9 = Conv2D(256, (3, 3), activation="relu", kernel_initializer="he_normal", padding='same')(up9) up10 = concatenate([ Conv2DTranspose(192, (3, 3), activation="relu", kernel_initializer="he_normal", strides=(2, 2), padding='same')(conv9), conv4 ], axis=3) conv10 = Conv2D(192, (3, 3), activation="relu", kernel_initializer="he_normal", padding='same')(up10) up11 = concatenate([ Conv2DTranspose(128, (3, 3), activation="relu", kernel_initializer="he_normal", strides=(2, 2), padding='same')(conv10), conv3 ], axis=3) conv11 = Conv2D(128, (3, 3), activation="relu", kernel_initializer="he_normal", padding='same')(up11) up12 = concatenate([ Conv2DTranspose(64, (3, 3), activation="relu", kernel_initializer="he_normal", strides=(2, 2), padding='same')(conv11), conv2 ], axis=3) conv12 = Conv2D(64, (3, 3), activation="relu", kernel_initializer="he_normal", padding='same')(up12) up13 = concatenate([ Conv2DTranspose(32, (3, 3), activation="relu", kernel_initializer="he_normal", strides=(2, 2), padding='same')(conv12), conv1 ], axis=3) conv13 = Conv2D(32, (3, 3), activation="relu", kernel_initializer="he_normal", padding='same')(up13) conv13 = Conv2D(1, (1, 1))(conv13) conv13 = Activation("sigmoid")(conv13) model = Model(img_input, conv13) return model
def training(): index_val = [] for x in range(0, 5): index_val.append(random.randint(1, 19)) X_train = [] X_valid = [] for j in range(1, 19): image_data_path = '/media/abhishek/New Volume/Kinect Project/data_mapped/sub' + str( j) + '/' for i in range(0, 130): w = 10000 + i st = str(w) file_name = image_data_path + 'MappedFrame' + st[1:] + '.jpg' img = cv2.imread(file_name) if img is None: continue else: if j in index_val: X_valid.append(img) else: X_train.append(img) X_train = np.array(X_train) X_valid = np.array(X_valid) y_data = pd.read_csv( '/media/abhishek/New Volume/Kinect Project/data_mapped/labels.csv') y = y_data.label y = np_utils.to_categorical(y) y_train = [] y_valid = [] for j in range(1, 19): if j in index_val: for i in range((j - 1) * 129, (j * 129)): y_valid.append(y[i][:]) else: for i in range((j - 1) * 129, (j * 129)): y_train.append(y[i][:]) print(np.shape(X_train)) print(np.shape(y_train)) print(np.shape(X_valid)) print(np.shape(y_valid)) X_train = preprocess_input(X_train, mode='tf') X_valid = preprocess_input(X_valid, mode='tf') base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) X_train = base_model.predict(X_train) X_valid = base_model.predict(X_valid) y_train = np.array(y_train) y_valid = np.array(y_valid) print(X_train.shape) X_train = X_train.reshape(1806, 7 * 7 * 512) X_valid = X_valid.reshape(516, 7 * 7 * 512) train = X_train / X_train.max() train = np.array(train) X_valid = X_valid / X_train.max() X_valid = np.array(X_valid) model = Sequential() model.add(InputLayer((7 * 7 * 512, ))) model.add( Dense(units=1024, input_shape=(7 * 7 * 512, ), activation='sigmoid')) model.add(Dense(3, input_shape=(1024, ), activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(train, y_train, epochs=20, validation_data=(X_valid, y_valid)) model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights("model.h5") print("Saved model to disk")
#print(get_img(ID)) img_, y_, cat_, cat__ = get_img(ID) X_img[i, ] = img_ y_label[i, y_.argmax() + 10 * cat_] = 1 X_cat[i, ] = cat__ #X_cat[i,cat_*10:cat_*10+10] = np.ones(10) return [ X_img, X_cat ], y_label #keras.utils.to_categorical(y, num_classes=self.n_classes) with tf.device('/device:' + device_str): if opt.net == 'VGG16': vgg_head = VGG16(weights='imagenet', include_top=False) vgg_head.name = 'vgg16_head' img_input = Input(shape=(224, 224, 3), name='image_input') cat_input = Input(shape=(80, ), name='cat_input') output_vgg16_conv = vgg_head(img_input) x = Flatten(name='flatten')(output_vgg16_conv) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(80, activation='softmax', name='shit')(x) x = multiply(([x, cat_input])) x = Softmax(80)(x) my_model = Model(inputs=[img_input, cat_input], output=x)
def extract_folds_features(argv): # Parse command line to read with which network should the script extract image information parser = argparse.ArgumentParser( description='Receives the desired network model for extraction') parser.add_argument( '--network', '-n', default="vgg16", help= 'The desired network. We only support vgg16, inceptionV3 and resnet50') parser.add_argument( '--pooling', '-p', default="GAP", help= 'Used pooling layer. We support: Global Max Poolling [GAP], Global Average Pooling[GMP], None [None]' ) parser.add_argument('--fold', '-f', default="all", help='Select which fold to extract features') args = parser.parse_args() # Check if network is suported # We now only have vgg16 if not ((args.network == "vgg16") or (args.network == "inceptionV3") or (args.network == "resnet50")): print_error( "We currently only suport the followuing models: vgg16, inceptionv3 and resnet50" ) exit(1) if not ((args.pooling == "GAP") or (args.pooling == "GMP") or (args.pooling == "None")): print_error("We currently only suport GAP, GMP and None layers") exit(1) # ------------------ Create extracting model inputs = Input(CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) # ------------------ Convolution ------------------ if args.network == "vgg16": convolutional_layer = VGG16( weights='imagenet', include_top=False, input_shape=CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) for layer in convolutional_layer.layers[:]: layer.trainable = False #Freezes all layers in the vgg16 output_shape = CONST_VEC_NETWORK_VGG16_OUTPUTSHAPE if args.network == "inceptionV3": convolutional_layer = InceptionV3( weights='imagenet', include_top=False, input_shape=CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) for layer in convolutional_layer.layers[:]: layer.trainable = False #Freezes all layers in the vgg16 output_shape = CONST_VEC_NETWORK_INCEPTIONV3_OUTPUTSHAPE if args.network == "resnet50": convolutional_layer = ResNet50( weights='imagenet', include_top=False, input_shape=CONST_VEC_DATASET_OUTPUT_IMAGE_SHAPE) for layer in convolutional_layer.layers[:]: layer.trainable = False #Freezes all layers in the vgg16 output_shape = CONST_VEC_NETWORK_RESNET50_OUTPUTSHAPE convolution_output = convolutional_layer(inputs) # ------------------ Pooling ------------------ if args.pooling == "None": outputs = convolution_output elif args.pooling == "GAP": outputs = GlobalAveragePooling2D(data_format=None)(convolution_output) elif args.pooling == "GMP": outputs = GlobalMaxPooling2D(data_format=None)(convolution_output) model = Model(inputs=inputs, outputs=outputs) print_info("Extracting network summary:") model.summary() # ------------------ Extracting process telegramSendMessage("Starting extracting process with network " + args.network + " and pooling layer " + args.pooling) print_info("Starting extracting process with network " + args.network + " and pooling layer " + args.pooling) # Create folder to host all extracted models if (args.network == "vgg16"): extraction_datapath = os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, CONST_STR_DATASET_FEATURES_VGG16) if (args.network == "inceptionV3"): extraction_datapath = os.path.join( CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, CONST_STR_DATASET_FEATURES_INCEPTIONV3) if (args.network == "resnet50"): extraction_datapath = os.path.join( CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, CONST_STR_DATASET_FEATURES_RESNET50) try: if not os.path.exists(extraction_datapath): os.makedirs(extraction_datapath) except OSError: print_error("Could not make directory for features extraction") telegramSendMessage("Error: Creating directory") exit(1) if args.fold == "all": for fold in folds: # Load fold dataset print_info("Loading dataset from " + fold["name"]) telegramSendMessage("Loading dataset from " + fold["name"]) try: input_train_data = np.load( os.path.join( CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_training_data_" + fold['name'] + ".npy")) input_test_data = np.load( os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_testing_data_" + fold['name'] + ".npy")) except: print_error("Could not find dataset. Did you build it?") telegramSendMessage( "Could not find dataset. Did you build it?") exit(1) # Extracting features of fold print_info("Extracting training features") telegramSendMessage("Extracting training features") train_features = [] number_of_images = input_train_data.shape[0] for index in range(number_of_images): # Expand dimention of image from (224,224,3) to (1,224,224,3) image = np.expand_dims(input_train_data[index], 0) # Pass image throught the model image_feature = model.predict(image) # Append to the train_features array train_features.append(image_feature) #Transform array into ndarray train_features = np.array(train_features).astype("float32") train_features = np.reshape(train_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving training features") telegramSendMessage("Saving training features") np.save( os.path.join( extraction_datapath, "input_training_data_" + args.pooling + "_" + fold["name"]), train_features) ### Repeat to test dataset print_info("Extracting testing features") telegramSendMessage("Extracting testing features") test_features = [] number_of_images = input_test_data.shape[0] for index in range(number_of_images): image = np.expand_dims(input_test_data[index], 0) image_feature = model.predict(image) test_features.append(image_feature) test_features = np.array(test_features).astype("float32") test_features = np.reshape(test_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving testing features") telegramSendMessage("Saving testing features") np.save( os.path.join( extraction_datapath, "input_testing_data_" + args.pooling + "_" + fold["name"]), test_features) # Forcefully delete input datas from memory del input_train_data del input_test_data del train_features del test_features gc.collect() else: fold_name = args.fold # Load fold dataset print_info("Loading dataset from " + fold_name) telegramSendMessage("Loading dataset from " + fold_name) try: input_train_data = np.load( os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_training_data_" + fold_name + ".npy")) input_test_data = np.load( os.path.join(CONST_STR_DATASET_BASE_PATH, CONST_STR_DATASET_DATAPATH, CONST_STR_DATASET_FOLDS_DATAPATH, "input_testing_data_" + fold_name + ".npy")) except: print_error("Could not find dataset. Did you build it?") telegramSendMessage("Could not find dataset. Did you build it?") exit(1) # Extracting features of fold print_info("Extracting training features") telegramSendMessage("Extracting training features") train_features = [] number_of_images = input_train_data.shape[0] for index in range(number_of_images): # Expand dimention of image from (224,224,3) to (1,224,224,3) image = np.expand_dims(input_train_data[index], 0) # Pass image throught the model image_feature = model.predict(image) # Append to the train_features array train_features.append(image_feature) #Transform array into ndarray train_features = np.array(train_features).astype("float32") train_features = np.reshape(train_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving training features") telegramSendMessage("Saving training features") np.save( os.path.join( extraction_datapath, "input_training_data_" + args.pooling + "_" + fold_name), train_features) ### Repeat to test dataset print_info("Extracting testing features") telegramSendMessage("Extracting testing features") test_features = [] number_of_images = input_test_data.shape[0] for index in range(number_of_images): image = np.expand_dims(input_test_data[index], 0) image_feature = model.predict(image) test_features.append(image_feature) test_features = np.array(test_features).astype("float32") test_features = np.reshape(test_features, (number_of_images, output_shape[2])) #Save the extracted features print_info("Saving testing features") telegramSendMessage("Saving testing features") np.save( os.path.join( extraction_datapath, "input_testing_data_" + args.pooling + "_" + fold_name), test_features) # Forcefully delete input datas from memory del input_train_data del input_test_data del train_features del test_features gc.collect() print_info("Extraction script end") telegramSendMessage("Extraction script end")
# 3) training model model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.fit_generator(generator=train_datagen.flow(X_train, y_train, batch_size=32), epochs=10, steps_per_epoch=len(X_train) / 32) # Part 2 : Using VGG16 to make a prediction # 1) loading and checking model vgg = VGG16(weights='imagenet', include_top=True) vgg.summary() # 2) preparing image img = img_to_array(load_img('hamster.jpg', target_size=(224, 224))) img = preprocess_input(img) # 3) adding a dimension img = np.expand_dims(img, axis=0) # 4) making prediction prediction = decode_predictions(vgg.predict(img)) print(prediction) # Part 3 : Fine-tuning VGG16 on a new dataset
xtas, ytas = [], [] for i in range(Xtrain.shape[0]): num_aug = 0 x = Xtrain[i][np.newaxis] datagen.fit(x) for x_aug in datagen.flow(x, batch_size=1): if num_aug >= NUM_TO_AUGMENT: break xtas.append(x_aug[0]) ytas.append(ytrain[i]) num_aug += 1 Xtrain = np.array(xtas) ytrain = np.array(ytas) print(Xtrain.shape, Xtest.shape, ytrain.shape, ytest.shape) # Instantiate VGG16 model and remove bottleneck print("Instantiating VGG16 model and removing top layers...") vgg16_model = VGG16(weights="imagenet", include_top=True) model = Model(input=vgg16_model.input, output=vgg16_model.get_layer("block5_pool").output) sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd, loss="categorical_crossentropy") # Read each of train, validation and test vectors out to named files print("Writing vectors to files...") write_vectors(model, Xtrain, ytrain, "train", DATA_DIR, BATCH_SIZE) write_vectors(model, Xtest, ytest, "test", DATA_DIR, BATCH_SIZE)
batch_size = 32 train_gen = ImageDataGenerator() training_set = train_gen.flow_from_directory( directory="../dataset/training_set", target_size=(224, 224), batch_size=batch_size) test_gen = ImageDataGenerator() test_set = test_gen.flow_from_directory(directory="../dataset/test_set", target_size=(224, 224), batch_size=batch_size) vggmodel = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) print(vggmodel.summary()) for layers in (vggmodel.layers): layers.trainable = False x = Flatten()(vggmodel.output) x = Dense(4096, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(0.7)(x) x = Dense(4096, activation='relu')(x) x = BatchNormalization()(x)
from keras.models import Model, Sequential from keras.layers import Dense, Flatten from config import Config as C from keras.applications.vgg16 import VGG16 from numpy import random #VGG16 seed = random.seed(300) vgg16 = VGG16(weights='imagenet', include_top=False, input_shape=(3, C.height, C.width)) # Generate a model with all layers (with top) output_model = Sequential() output_model.add(Flatten()) output_model.add(Dense(4096, activation='relu')) output_model.add(Dense(C.classes, activation='softmax', name='predictions')) #Then create the corresponding model # this works but layers of output model are not shown # model = Model(input=vgg16.input, output=output_model(vgg16.output)) model = Sequential() #model is the vgg16 architecture with modified output for l in vgg16.layers: model.add(l) for l in output_model.layers: model.add(l)
def __init__(self, input_size): vgg16 = VGG16(input_shape=(input_size, input_size, 3), include_top=False) #vgg16.load_weights(VGG16_BACKEND_PATH) self.feature_extractor = vgg16
decay = 0.0001 adam = Adam(lr=lr, decay=decay) # Load dataset train_data_path = "/dataset/train_data.npy" train_label_path = "/dataset/train_labels_cate.npy" val_data_path = "/dataset/val_data.npy" val_label_path = "/dataset/val_labels_cate.npy" x_train = load.load_dataset(train_data_path) y_train = load.load_dataset(train_label_path) x_val = load.load_dataset(val_data_path) y_val = load.load_dataset(val_label_path) #setup vgg cnn vgg = VGG16(include_top=False, weights='imagenet', input_shape=(72, 72, 3)) cnn = Sequential() cnn.add(vgg) cnn.add(Dropout(0.5)) cnn.add(Flatten()) #add LSTM to model model = Sequential() model.add( TimeDistributed(cnn, input_shape=(seq_length, 72, 72, 3), trainable=False)) model.add( LSTM(128, input_shape=(batch_size, seq_length, 1), return_sequences=True)) model.add(TimeDistributed(Dropout(0.5))) # add FC model.add(TimeDistributed(Dense(64)))
img_rows = 150 img_cols = 125 channels = 3 train_data_dir = '/Users/corno/Python/imgAnalysis/imgSleeves/train' validation_data_dir = '/Users/corno/Python/imgAnalysis/imgSleeves/validation' epochs = 50 result_dir = '/Users/corno/Python/imgAnalysis/imgSleeves/results' if not os.path.exists(result_dir): os.mkdir(result_dir) input_tensor = Input(shape=(img_rows, img_cols, channels)) base_model = VGG16(include_top=False, weights='imagenet', input_tensor=input_tensor) base_model.summary() x = base_model.output x = Flatten(input_shape=base_model.output_shape[1:])(x) x = Dense(256, activation='relu')(x) x = Dropout(0.5)(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) model.summary() model.layers[:15] # 最後のconv層の直前までの層をfreeze
test_tabular = test[dummy_tabular_cols] test_text = test[dummy_text_cols] test_labels = test[dummy_labels_cols] test_generator = TextMultimodalDataGenerator( test_images, test_tabular, test_text, test_labels, batch_size=BATCH_SIZE, target_size=(IMAGE_SIZE, IMAGE_SIZE), directory=DATASET_PATH + "images/" ) # Build image model base_model1 = VGG16(weights='imagenet', include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) for layer in base_model1.layers: layer.trainable = False x1 = base_model1.output x1 = GlobalAveragePooling2D()(x1) x1 = Dropout(DROPOUT_PROB)(x1) log_file = open(log_name, 'w') log_file.write('VGG/Tabular Late Fusion \n') base_model1.summary(print_fn=lambda x: log_file.write(x + '\n\n')) # Build tabular model base_model2 = Sequential()
# -*- coding: utf-8 -*- """ Deep Learning Türkiye topluluğu için Mert Çobanoğlu tarafından hazırlanmıştır. Amaç: Keras ile nesne tanıma. Algoritma: Evrişimli Sinir Ağları (Convolutional Neural Networks) Ek: Çalışma ile ilgili rehber README.md dosyasında belirtilmiştir. """ from keras.applications.vgg16 import VGG16 from keras.preprocessing import image from keras.applications.vgg16 import preprocess_input, decode_predictions import numpy as np model = VGG16(weights='imagenet') img_path = 'images/bird.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('Predicted:', decode_predictions(preds, top=3)[0])
from keras.applications.vgg16 import VGG16 from keras.models import Sequential from keras.layers import Conv2D, MaxPool2D, Activation, Dropout, Flatten, Dense from keras.optimizers import SGD from keras.optimizers import adam from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img import numpy as np vgg16_model = VGG16(weights='imagenet', include_top=False, input_shape=(513, 45, 3)) # 搭建全连接层 top_model = Sequential() top_model.add(Flatten(input_shape=vgg16_model.output_shape[1:])) top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(2, activation='softmax')) model = Sequential() model.add(vgg16_model) model.add(top_model) train_datagen = ImageDataGenerator( width_shift_range=0.2, # 随机水平平移 height_shift_range=0.2, # 随机竖直平移 rescale=1 / 255, # 数据归一化 horizontal_flip=True, # 水平翻转 ) test_datagen = ImageDataGenerator( rescale=1 / 255, # 数据归一化 )
from __future__ import print_function import time import numpy as np import matplotlib matplotlib.use('TKAgg') import matplotlib.pyplot as plt from PIL import Image from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions from keras.preprocessing import image from keras.applications.vgg16 import VGG16 vgg16 = VGG16(weights='imagenet', include_top=True) #print(vgg16) # Load the cat image and resize it to 224x224 img = image.load_img('image_0001.jpg', target_size=(224, 224)) # Convert to Numpy array (224, 224, 3) x = image.img_to_array(img) # Add an empty dimension in front to obtain a tensor (1, 224, 224, 3) x = np.expand_dims(x, axis=0) # Perform mean removal as in the original VGG16 network x = preprocess_input(x) # Make the prediction using VGG16 output = vgg16.predict(x) #print(output) plt.figure() plt.plot(output[0, :], 'o') plt.show()
from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras import optimizers nb_classes = 20 base_dir = '.' prefix = 'vgg16' n_train_samples = 21027 train_file_name = 'bottleneck_features_train.npy' n_validation_samples = 1000 validation_file_name = 'bottleneck_features_validation.npy' # VGG16(model & weight)をインポート model = VGG16(include_top=False, weights='imagenet') model.summary() # 画像データをnumpy arrayに変換 ## training dataの読み込み image_data_generator = ImageDataGenerator(rescale=1.0/255) train_data = image_data_generator.flow_from_directory( 'Dataset/traindat', target_size=(150, 150), batch_size=32, class_mode=None, shuffle=False ) ## validation dataの読み込み image_data_generator = ImageDataGenerator(rescale=1.0/255)
img_rows = 100 img_cols = 100 train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_generator = train_datagen.flow_from_directory(sys.argv[1], target_size=(100, 100), color_mode='rgb', batch_size=16, class_mode='categorical') b_model = VGG16(weights='imagenet', include_top=False, input_shape=(100, 100, 3)) op_layer = b_model.output f_layer = Flatten()(op_layer) d_layer = Dense(101)(f_layer) a_layer = Activation('softmax')(d_layer) model = Model(inputs=b_model.input, output=a_layer) model = load_model(sys.argv[2]) for layer in model.layers[:18]: layer.trainable = False model.compile(loss='categorical_crossentropy', optimizer='adamax', metrics=['accuracy'])
# Installing Keras # pip install --upgrade keras # Part 1 - Building the CNN # Importing the Keras libraries and packages from keras.applications.vgg16 import VGG16 from keras.layers import Dense, GlobalAveragePooling2D from keras.models import Model import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' base_model = VGG16(weights='imagenet', include_top=False, input_shape=(128, 128, 3)) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) x = Dense(1024, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) for layer in base_model.layers: layer.trainable = False model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers
print('y_train shape:', y_train.shape) # Convert class vectors to binary class matrices. y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) if args.resume: print('resume from checkpoint') model = keras.models.load_model(save_file) else: print('train from start') model = models.Sequential() if '16' in args_model: base_model = VGG16(weights=None, include_top=False, input_shape=(32, 32, 3), pooling=None) elif '19' in args_model: base_model = VGG19(weights=None, include_top=False, input_shape=(32, 32, 3), pooling=None) #base_model.summary() #pdb.set_trace() model.add(base_model) model.add(layers.Flatten()) model.add(layers.BatchNormalization()) model.add(layers.Dense(
from os import listdir import time from pickle import dump from keras.preprocessing.image import load_img, img_to_array from keras.applications.vgg16 import VGG16, preprocess_input from keras.layers import Input inp = Input(shape=(224, 224, 3)) model = VGG16(include_top=False, input_tensor=inp) features = dict() for file in listdir('Flicker8k_Dataset'): start = time.time() image_name = 'Flicker8k_Dataset/' + file image = load_img(image_name, 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) features[file.split('.')[0]] = feature print(time.time() - start) dump(features, open('features.pkl', 'wb'))