import tensorflow as tf import keras import matplotlib.pyplot as plt import numpy as np from keras.applications.inception_v3 import InceptionV3, decode_predictions from keras import backend as K iv3 = InceptionV3() from keras.preprocessing import image x = image.img_to_array(image.load_img('hacked.png', target_size=(299, 299))) #cambio de rango de 0 - 255 a -1 - 1 x /= 255 x -= 0.5 x *= 2 x = x.reshape([1, x.shape[0], x.shape[1], x.shape[2]]) y = iv3.predict(x) y.shape print(decode_predictions(y)) #ataque adversario inp_layer = iv3.layers[0].input out_layer = iv3.layers[-1].output target_class = 951 loss = out_layer[0, target_class]
#use triplet identify the vehicle from keras.applications.inception_v3 import InceptionV3 from keras.callbacks import EarlyStop, ReduceLROnPlateau, ModelCheckpoint from keras.layers import Dense, Input, Lambda from keras.models import Model LEARNING_RATE = 0.00001 IMG_WIDTH = 299 IMG_HEIGHT = 299 NBR_MODELS = 250 NBR_COLORS = 7 INITIAL_EPOCH = 0 #define the model, we get imagenet weights from InceptionV3, but don't need the top layer inception = InceptionV3(include_top = False, input_tensor = None, input_shape= (IMG_WIDTH, IMG_HEIGHT, 3), pooling = 'avg') f_base = inception.get_layers(index = -1).output f_acs = Dense(1024, name='f_acs')(f_base) feature_model = Model(inputs = inception.input, outputs = f_acs) ancor = Input(input_shape= (IMG_WIDTH, IMG_HEIGHT, 3), name='ancor') positive = Input(input_shape= (IMG_WIDTH, IMG_HEIGHT, 3), name='positive') negative = Input(input_shape= (IMG_WIDTH, IMG_HEIGHT, 3), name='negative') #the function of classify the car model and color is only for the ancor #now get the ancor f_acs layers feature #after this layer, will do model classify and color classify f_acs_ancor = feature_model(ancor) f_ancor_model = Dense(NBR_MODELS, activation='softmax', name='pred_model')(f_acs_ancor) f_ancor_color = Dense(NBR_COLORS, activation='softmax', name = 'pred_color')(f_acs_ancor)
cat002.jpg ... ``` ''' from keras.applications.inception_v3 import InceptionV3 from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras.preprocessing.image import ImageDataGenerator from keras import backend as K from keras.callbacks import ModelCheckpoint from keras.callbacks import TensorBoard import os.path # create the base pre-trained model base_model = InceptionV3(weights='imagenet', include_top=False) # dimensions of our images. #Inception input size img_width, img_height = 299, 299 top_layers_checkpoint_path = 'cp.top.best.hdf5' fine_tuned_checkpoint_path = 'cp.fine_tuned.best.hdf5' new_extended_inception_weights = 'final_weights.hdf5' train_data_dir = 'data/train' validation_data_dir = 'data/validation' nb_train_samples = 2000 nb_validation_samples = 800
def load_vanilla_model(): # return ResNet50(weights='imagenet', include_top=True) return InceptionV3(weights='imagenet', include_top=True)
HEIGHT = 1080 DIMS = 3 K = list(range(2048)) R = list( range( len([ filename for filename in os.listdir(f"Analysis/{image_path}/") if ".jpg" in filename ]))) # Create convolutional network. convolutional_model = InceptionV3( include_top=False, weights='imagenet', input_tensor=None, input_shape=(WIDTH, HEIGHT, DIMS), ) output = GlobalAveragePooling2D()(convolutional_model.output) convolutional_model = Model(inputs=convolutional_model.inputs, outputs=output) convolutional_model.summary() # Create .csv. image_data = pd.DataFrame(columns=["id"] + K) if "images.npy" not in os.listdir(f"Analysis/"): for id in tqdm(R): img = skimage.io.imread(f"Analysis/{image_path}/{id}.jpg") img = resize(img, (WIDTH, HEIGHT))
from keras.applications.inception_v3 import InceptionV3 from glob import glob # In[2]: import os data_path = os.path.abspath('C:/Users/Input/gaussian_filtered_images/gaussian_filtered_images') # In[3]: v3 = InceptionV3(input_shape=[224,224,3], weights='imagenet', include_top=False) # In[4]: # For not training the VGG weights for layer in v3.layers: layer.trainable = False # In[5]: x = Flatten()(v3.output) prediction = Dense(5, activation='softmax')(x)
from keras.preprocessing.image import img_to_array, load_img from keras.applications.inception_v3 import preprocess_input as inception_preprocess from keras.applications.resnet50 import preprocess_input as resnet_preprocess from keras.applications.inception_v3 import InceptionV3 from keras.applications.resnet50 import ResNet50 from keras.models import Model, load_model from keras.layers import Input from keras.applications.imagenet_utils import decode_predictions from image_processor import inception_image_processor, resnet_image_processor import numpy as np imgpath = 'Clean_Data/Train/Archery/v_Archery_g01_c01/image-0001.jpg' model = InceptionV3(weights='imagenet', include_top=True) img = load_img(imgpath, target_size=(299, 299, 3)) vec = img_to_array(img) vec = np.expand_dims(vec, axis=0) vec = inception_preprocess(vec) print(vec.shape) preds = model.predict(vec) print(decode_predictions(preds, top=3)[0])
def GoogleInceptionV3(): model = InceptionV3(weights='imagenet', include_top=False) model.trainable = False return model
width_shift_range=0.2, height_shift_range=0.2) train_gen = data_gen.flow_from_directory('../../data/train', color_mode='rgb', batch_size=batch_size, target_size=(299, 299)) valid_gen = data_gen.flow_from_directory('../../data/validation', color_mode='rgb', batch_size=batch_size, target_size=(299, 299)) ### Modeling base_model = InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000) x = base_model.layers[-6].output predictions = Dense(5, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) for layer in model.layers[:-5]: layer.trainable = False model.summary() ### Train the model model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# loading dataset train_faces, train_emotions, val_faces, val_emotions = load_fer2013() num_samples, num_classes = train_emotions.shape train_faces /= 255. val_faces /= 255. #### My part - try building in inception and retrain last layer # setup model base_model = InceptionV3( weights='imagenet', include_top=False) #include_top=False excludes final FC layer model = add_new_last_layer(base_model, num_classes) # fine-tuning setup_to_finetune(model) # preprocess - apply in the image generator to ge tto Inception input size IM_HEIGTH datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rescale=IM_HEIGHT / input_shape[0]) # retrain # MJH Get this to work here! train_faces = np.repeat(train_faces[:, :, :, 0], 3, axis=3) print(" train-faces: {:} train-emotions: {:}".format(train_faces.shape, train_emotions.shape))
#import pandas as pd import numpy as np import os import keras import matplotlib.pyplot as plt from keras.layers import Dense, GlobalAveragePooling2D from keras.applications.inception_v3 import InceptionV3 from keras.preprocessing import image from keras.applications.mobilenet import preprocess_input from keras.preprocessing.image import ImageDataGenerator from keras.models import Model from keras.optimizers import Adam base_model = InceptionV3( weights='imagenet', include_top=False ) #imports the mobilenet model and discards the last 1000 neuron layer. x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')( x ) #we add dense layers so that the model can learn more complex functions and classify for better results. x = Dense(1024, activation='relu')(x) #dense layer 2 x = Dense(512, activation='relu')(x) #dense layer 3 preds = Dense(2, activation='softmax')(x) #final layer with softmax activation model = Model(inputs=base_model.input, outputs=preds) #specify the inputs #specify the outputs #now a model has been created based on our architecture
from keras.applications.inception_v3 import InceptionV3 # from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras import backend as K K.clear_session() # Set the number of classes for the final layer CLASS_NUM = 120 inception = InceptionV3(include_top=False, weights='imagenet') # add a global spatial average pooling layer x = inception.output x = GlobalAveragePooling2D()(x) # fully-connected layer, relu x = Dense(1024, activation='relu')(x) # final layer fully connected, logistic predictions = Dense(CLASS_NUM, activation='softmax')(x) model = Model(inputs=inception.input, outputs=predictions) # train only the top layers for layer in inception.layers: layer.trainable = False model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
# InceptionV3/VGG19 needs image to have 3 channels i.e. RGB channel, since our images are in # grayscale this won't work but our ImageDataGenerator can load the images in 'rgb' that could help loaded_data = Loader.PreProcess(mode='rgb') train_X, train_Y = next(loaded_data.train_generator) print(train_X.shape) print(train_Y.shape) ''' Attempt 1: with 128,128 Image size and InceptionV3 ImageNet model transfer learning - Softmax Activation function for output layer Attempt 2: with 128,128 Image size and InceptionV3 ImageNet model transfer learning - Sigmoid Activation function for ouput layer ''' attempt = 2 # create the base pre-trained model base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=train_X.shape[1:]) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # let's add a fully-connected layer x = Dense(1024, activation='relu')(x) predictions = Dense(len(loaded_data.prediction_labels), activation='sigmoid')(x) chestr_transfer_cnn_model = Model(inputs=base_model.input, outputs=predictions) chestr_transfer_cnn_model.summary() # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers
def train_network(net='MobileNet_50'): # specify a directory to store model weights mpath = '/data3/eirini/gleason_CNN/models/finetune_%s' % net if not os.path.exists(mpath): os.makedirs(mpath) # specify the input directory, where image patches live prefix = '/data3/eirini/dataset_TMA' patch_dir = os.path.join(prefix, 'train_validation_patches_750') init_dim = 250 if net != 'InceptionV3' else 350 dim = 224 if net != 'InceptionV3' else 299 target_size = (dim, dim) input_shape = (target_size[0], target_size[1], 3) bs = 32 # classes class_labels = ['benign', 'gleason3', 'gleason4', 'gleason5'] n_class = len(class_labels) # training set train_filenames, train_classes = [], [] for tma in ['ZT199', 'ZT204', 'ZT111']: csv_path = os.path.join(prefix, 'tma_info', '%s_gleason_scores.csv' % tma) new_filenames, new_classes = get_filenames_and_classes(csv_path) train_filenames += new_filenames train_classes.append(new_classes) train_classes = np.vstack(train_classes) # validation set tma = 'ZT76' csv_path = os.path.join(prefix, 'tma_info', '%s_gleason_scores.csv' % tma) val_filenames, val_classes = get_filenames_and_classes(csv_path) # total number of TMA spots in training set N = len(train_filenames) print('Total training TMAs: %d' % N) # customize the pre-processing if net.startswith('MobileNet') or net.startswith('Inception'): preprocess_mode = 'tf' else: preprocess_mode = 'caffe' # define data generators train_batches = balanced_generator( patch_dir, filenames=train_filenames + train_filenames, classes=np.hstack([train_classes[:, 0], train_classes[:, 1]]), input_dim=init_dim, crop_dim=dim, batch_size=bs, data_augmentation=True, save_to_dir=None, add_classes=True, preprocess_mode=preprocess_mode) valid_batches = balanced_generator(patch_dir, filenames=val_filenames, classes=val_classes[:, 0], input_dim=init_dim, crop_dim=dim, batch_size=bs, data_augmentation=False, save_to_dir=None, add_classes=True, preprocess_mode=preprocess_mode) model_weights = os.path.join(mpath, 'model_{epoch:02d}.h5') # get the model architecture print('Training %s ...' % net) if net == 'VGG16': base_model = VGG16(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg') elif net == 'ResNet50': base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg') elif net == 'InceptionV3': # original size: 299x299 base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg') elif net == 'MobileNet_100': base_model = MobileNet(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg', alpha=1.0, depth_multiplier=1, dropout=.2) elif net == 'MobileNet_75': base_model = MobileNet(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg', alpha=.75, depth_multiplier=1, dropout=.2) elif net == 'MobileNet_50': base_model = MobileNet(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg', alpha=.5, depth_multiplier=1, dropout=.2) elif net == 'MobileNet_25': base_model = MobileNet(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg', alpha=.25, depth_multiplier=1, dropout=.2) elif net == 'DenseNet121': tf_weights = 'cnn_finetune/imagenet_models/densenet121_weights_tf.h5' base_model = densenet121_model(dim, dim, 3, dropout_rate=0.2, weights_path=tf_weights) else: print('Unknown model, will train MobileNet instead.') base_model = MobileNet(include_top=False, weights='imagenet', input_shape=(dim, dim, 3), pooling='avg', alpha=.5, depth_multiplier=1, dropout=.2) # add top layer and compile model x_top = base_model.output x_out = Dense(n_class, name='output', activation='softmax')(x_top) model = Model(base_model.input, x_out) model.summary() # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional layers for layer in base_model.layers: layer.trainable = False model.compile(optimizer=optimizers.Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy']) history = model.fit_generator(generator=train_batches, steps_per_epoch=100, epochs=5, validation_data=valid_batches, validation_steps=100, verbose=1) # let the layers train freely for layer in model.layers: layer.trainable = True model.compile(optimizer=optimizers.SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) checkpoint = ModelCheckpoint(filepath=model_weights, save_best_only=False, verbose=0) reduce_lr = ReduceLROnPlateau(monitor='loss', factor=0.2, patience=10, min_lr=0.00001) history = model.fit_generator(generator=train_batches, steps_per_epoch=100, epochs=500, callbacks=[checkpoint, reduce_lr], validation_data=valid_batches, validation_steps=100, verbose=1) # save the full training history with open(os.path.join(mpath, 'history.pkl'), 'wb') as history_f: pickle.dump(history.history, history_f, protocol=2) # save the best model in terms of validation loss best_epoch_idx = np.argmin(history['val_loss']) best_model_weights = os.path.join(mpath, 'model_{:02d}.h5'.format(best_epoch_idx)) copyfile(best_model_weights, os.path.join(mpath, 'best_model_weights.h5'))
def Inception(self, params): self.ids_inputs = params["INPUTS_IDS_MODEL"] self.ids_outputs = params["OUTPUTS_IDS_MODEL"] activation_type = params['CLASSIFIER_ACTIVATION'] nOutput = params['NUM_CLASSES'] # Load Inception model pre-trained on ImageNet self.model = InceptionV3(weights='imagenet') # Freeze the base model # self.model.trainable = False # Recover input layer image = self.model.get_layer(self.ids_inputs[0]).output # Convolution2D layers conv1 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')(image) conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1) conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1) conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2) conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool2) conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3) conv3 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool3) conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4) conv4 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) # Middle of the path (bottleneck) conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool4) conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5) conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(conv5) # Upsampling path up_conv5 = UpSampling2D(size=(2, 2))(conv5) up_conv5 = ZeroPadding2D()(up_conv5) # up6 = Concat(cropping=[None, None, 'center', 'center'])([conv4, up_conv5]) conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(up_conv5) conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6) conv6 = Conv2D(256, (3, 3), activation='relu', padding='same')(conv6) up_conv6 = UpSampling2D(size=(2, 2))(conv6) up_conv6 = ZeroPadding2D()(up_conv6) # up7 = Concat(cropping=[None, None, 'center', 'center'])([conv3, up_conv6]) conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(up_conv6) conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7) conv7 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv7) up_conv7 = UpSampling2D(size=(2, 2))(conv7) up_conv7 = ZeroPadding2D()(up_conv7) # up8 = Concat(cropping=[None, None, 'center', 'center'])([conv2, up_conv7]) conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(up_conv7) conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8) conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(conv8) up_conv8 = UpSampling2D(size=(2, 2))(conv8) up_conv8 = ZeroPadding2D()(up_conv8) # up9 = Concat(cropping=[None, None, 'center', 'center'])([conv1, up_conv8]) conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(up_conv8) conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9) conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv9) # Final classification layer (batch_size, classes, width, height) x = Conv2D(params['NUM_CLASSES'], (1, 1), border_mode='same')(conv9) # Create last layer (classification) x = Dense(nOutput, activation=activation_type)(x) x = Dense(2000, activation='tanh')(x) x = Dense(1000, activation='tanh')(x) x = Dense(500, activation='tanh')(x) # output out = Activation(params['CLASSIFIER_ACTIVATION'], name=self.ids_outputs[0])(x) # instantiate model self.model = Model(inputs=image, outputs=out) # compile self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=[ 'categorical_accuracy']) # summary self.model.summary()
from data_manipulation import train_gen, valid_gen, test_gen import matplotlib.pyplot as plt import pandas as pd import numpy as np from math import ceil from keras.applications.inception_v3 import InceptionV3 from keras.layers import Dense, Dropout, Flatten, GlobalAveragePooling2D from keras.models import Sequential from keras.callbacks import EarlyStopping, ModelCheckpoint, CSVLogger import keras.regularizers as regularizers import keras.optimizers as optimizers inception = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) model = Sequential() model.add(inception) model.add(GlobalAveragePooling2D()) # model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.4)) model.add( Dense(101, activation='softmax', kernel_regularizer=regularizers.l2(0.005))) opt = optimizers.Adam(lr=0.0001) model.compile(optimizer=opt, loss='categorical_crossentropy',
test_generator = test_datagen.flow_from_directory(directory=r"./TEST_set/", target_size=(472, 696), color_mode="rgb", batch_size=1, class_mode=None, shuffle=False, seed=42) # initialize our VGG-like Convolutional Neural Network #model = SmallVGGNet.build(width=696, height=472, depth=3,classes=4) if GPUS != 1: tf.device('/cpu:0') input_tensor = Input(shape=(472, 696, 3)) model = InceptionV3(include_top=True, weights=None, input_shape=None, input_tensor=input_tensor, pooling=None, classes=3) #model = Sequential() #model.add(Activation("relu", input_shape=(height, width,1))) #model.add(Conv2D(128, (3,3), activation="relu", kernel_initializer="glorot_normal", strides=(1,1), input_shape=(472,696,3), padding="same", data_format="channels_last") ) #model.add(Conv2D(128, (3,3), activation="relu", kernel_initializer="glorot_normal", strides=(1,1), input_shape=(472,696,3), padding="same", data_format="channels_last") ) #model.add(MaxPooling2D(pool_size=(2, 2), strides=(1,1))) #model.add(Flatten()) #model.add(Dense(int(Nouts/5), activation='relu')) #model.add(Dense(int(Nouts/5), activation='relu')) #model.add(Dense(int(Nouts/5), activation='relu')) #model.add(Dense(int(Nouts/5), activation='relu')) #model.add(Dense(3, activation='softmax')) #model.add(BatchNormalization()) #model = SmallVGGNet.build(width=696, height=472, depth=3,classes=3)
def main(): parser = argparse.ArgumentParser() parser.add_argument("expt_dir", type=str, help="Path containing (train, val, test).json") parser.add_argument("out_prefix", type=str, help="Prefix for writing model and prediction") parser.add_argument("-e", "--epochs", type=int, default=10, help="# Epochs") parser.add_argument("-i", "--image_size", type=int, default=250, help="Image Size NxN") parser.add_argument("-m", "--model", type=str, choices=['inception', 'resnet'], default='resnet', help="Image Model") parser.add_argument("-b", "--batch_size", type=int, default=64, help="Batch size") parser.add_argument("-d", "--device", type=int, default=0, help="GPU device") parser.add_argument("-r", "--retrain", default=False, action='store_true', help="Retrains all layers (instead of last few layers") parser.add_argument( "-a", "--augment", type=int, default=0, help= "Augment data s.t. each attribute appears at least these many times") parser.add_argument("-c", "--class_weight", default=False, action='store_true', help="Use class weights during training") args = parser.parse_args() params = vars(args) os.environ['CUDA_VISIBLE_DEVICES'] = str(params['device']) # Load Data -------------------------------------------------------------------------------------------------------- train_path = osp.join(SEG_ROOT, 'annotations', params['expt_dir'], 'train2017.json') val_path = osp.join(SEG_ROOT, 'annotations', params['expt_dir'], 'val2017.json') train_anno_full = json.load(open(train_path)) train_anno = train_anno_full['annotations'] val_anno_full = json.load(open(val_path)) val_anno = val_anno_full['annotations'] anno_full = dict(train_anno.items() + val_anno.items()) print '# Train images = ', len(train_anno) print '# Val images = ', len(val_anno) print '# ALL images = ', len(anno_full) # Helpers --------------------------------------------------------------------------------------------------------- image_index = get_image_id_info_index() attr_id_to_name = load_attributes_shorthand() image_ids = sorted(train_anno.keys()) attr_ids_set = set() for img_id, entry in anno_full.iteritems(): for attr_entry in entry['attributes']: attr_ids_set.add(attr_entry['attr_id']) attr_ids = sorted(attr_ids_set) attr_names = [attr_id_to_name[attr_id] for attr_id in attr_ids] n_img = len(image_ids) n_attr = len(attr_ids) attr_id_to_idx = dict(zip(attr_ids, range(n_attr))) idx_to_attr_id = {v: k for k, v in attr_id_to_idx.iteritems()} print '# Images = ', n_img print '# Attributes = ', n_attr print 'Attributes: ' print attr_ids target_image_size = (params['image_size'], params['image_size']) print 'Loading Train Data' (x_train, y_train, image_id_train) = anno_to_data(train_anno, attr_id_to_idx, target_image_size) print '\nLoading Val Data' (x_val, y_val, image_id_val) = anno_to_data(val_anno, attr_id_to_idx, target_image_size) # Data Augmentation ------------------------------------------------------------------------------------------------ if params['augment'] > 0: # Current Stats counter = Counter() anno_dct = train_anno for idx, (image_id, entry) in enumerate(anno_dct.iteritems()): this_attr_ids = set() for attr_entry in entry['attributes']: this_attr_ids.add(attr_entry['attr_id']) for attr_id in this_attr_ids: counter[attr_id] += 1 min_count = params[ 'augment'] # Each attribute should appear at least these many times x_train_aug, y_train_aug = [], [] # Which attributes do we need to augment for? attr_ids_aug = filter(lambda x: counter[x] < min_count, attr_ids) print 'Augmenting data to {} for {} attributes:\n{}'.format( min_count, len(attr_ids_aug), attr_ids_aug) for attr_id in attr_ids_aug: num_remaining = min_count - counter[attr_id] attr_idx = attr_id_to_idx[attr_id] all_labels = y_train[:, attr_idx] # Get labels for all images train_idxs = np.where( all_labels > 0)[0] # Images which contains this attribute for i in range(num_remaining): # Select a random image from x_train row_idx = np.random.choice(train_idxs) # Add to augmented set x_train_aug.append(x_train[row_idx]) y_train_aug.append(y_train[row_idx]) x_train_aug = np.asarray(x_train_aug) y_train_aug = np.asarray(y_train_aug) print '# Augmented Rows = ', x_train_aug.shape[0] x_train = np.concatenate((x_train, x_train_aug), axis=0) y_train = np.concatenate((y_train, y_train_aug), axis=0) # Class Weights ---------------------------------------------------------------------------------------------------- class_weight = np.ones((n_attr, )) if params['class_weight']: n_samples, n_classes = y_train.shape for i in range(n_attr): class_weight[i] = n_samples / (n_classes * np.sum(y_train[:, i])) # Image Preprocessing --------------------------------------------------------------------------------------------- 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(x_train) # Model ------------------------------------------------------------------------------------------------------------ if params['model'] == 'inception': base_model = InceptionV3(weights='imagenet', include_top=False) elif params['model'] == 'resnet': base_model = ResNet50(weights='imagenet', include_top=False) else: raise ValueError('Unrecognized model') # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # let's add a fully-connected layer x = Dense(1024, activation='relu')(x) predictions = Dense(n_attr, activation='sigmoid')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers if not params['retrain']: for layer in base_model.layers: layer.trainable = False # compile the model (should be done *after* setting layers to non-trainable) model.compile(optimizer='rmsprop', loss='binary_crossentropy') # train the model on the new data for a few epochs epochs = params['epochs'] batch_size = params['batch_size'] print 'Training with #Epochs = {}, Batch size = {}'.format( epochs, batch_size) model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=len(x_train) / batch_size, epochs=epochs, class_weight=class_weight) model_out_path = params['out_prefix'] + '.h5' model.save(model_out_path) # Evaluate --------------------------------------------------------------------------------------------------------- print print 'Evaluating Model...' batch_size = params['batch_size'] n_val_rows = x_val.shape[0] preds = model.predict_generator(datagen.flow(x_val, batch_size=batch_size, shuffle=False), steps=(n_val_rows / batch_size) + 1, verbose=1) # preds = model.predict(x_val, verbose=1) ap_scores = average_precision_score(y_val, preds, average=None) attr_id_score = zip(attr_ids, ap_scores) for attr_id, attr_score in sorted(attr_id_score, key=lambda x: -x[1]): print '{:>20s}: {:.3f}'.format(attr_id_to_name[attr_id], attr_score) print 'C-MAP = ', np.mean(ap_scores) # Write Predictions ------------------------------------------------------------------------------------------------ out_path = params['out_prefix'] + '.json' predictions = [] n_val_rows = preds.shape[0] thresh = 0.1 for row_idx in range(n_val_rows): this_region_probs = preds[row_idx] pred_idxs = np.where(this_region_probs >= thresh)[0] image_id = image_id_val[row_idx] h, w = anno_full[image_id]['image_height'], anno_full[image_id][ 'image_width'] bimask = np.ones((h, w), order='F', dtype='uint8') rle = mask_utils.encode(bimask) del bimask for pred_idx in pred_idxs: attr_id = idx_to_attr_id[pred_idx] predictions.append({ 'image_id': image_id_val[row_idx], 'attr_id': attr_id, 'segmentation': rle, 'score': this_region_probs[pred_idx].astype(float), }) print 'Writing {} predictions'.format(len(predictions)) json.dump(predictions, open(out_path, 'w'), indent=2)
def __init__(self): self.cnn_model_name = "InceptionV3" model = InceptionV3(weights='imagenet') input_layer = model.input hidden_layer = model.layers[-2].output self.my_model = Model(input_layer, hidden_layer)
MODEL_HDF5 = DATASET_ROOT_PATH + 'pretrain/agegender_' + ANNOTATIONS + '_' + MODELS + '_' + DATASET_NAME + AUGUMENT + '.hdf5' #Size if ANNOTATIONS == 'age': N_CATEGORIES = 8 if ANNOTATIONS == 'gender': N_CATEGORIES = 2 if ANNOTATIONS == 'age101': N_CATEGORIES = 101 #model if (MODELS == 'inceptionv3'): IMAGE_SIZE = 299 input_tensor = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=input_tensor) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) predictions = Dense(N_CATEGORIES, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) layer_num = len(model.layers) for layer in model.layers[:279]: layer.trainable = False for layer in model.layers[279:]: layer.trainable = True elif (MODELS == 'vgg16'):
from keras.layers import Dense, Lambda, Dropout, GlobalAveragePooling2D from keras.callbacks import CSVLogger, ModelCheckpoint import os from keras.applications.inception_v3 import InceptionV3, preprocess_input from keras.optimizers import SGD, RMSprop epochs = 500 batch_size = 64 widths, heights = 299, 299 # 训练集、验证集、测试集严密隔离 train_set = get_picture("train/tumor/origin_images/") # 训练集数据文件夹 valid_set = get_picture("validation/") # 验证集数据文件夹 mask_pictures = get_picture("train/tumor/annotation_images/") # 所有的mask图文件夹 base_model = InceptionV3(weights=None, include_top=False, input_shape=(widths, heights, 3), classes=2) # 迁移学习,载入InceptionV3的权重,拿来直接用 x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) # new FC layer, random init x = Dropout(0.5)(x) predictions = Dense(2, activation='softmax')(x) # new softmax layer model = Model(inputs=base_model.input, outputs=predictions) for layer in base_model.layers: layer.trainable = True model.compile(optimizer=SGD(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy']) #model = load_model('model_1000_8_13.h5') #model.compile(optimizer=SGD(lr=0.00007, momentum=0.5, decay=0.05), loss='categorical_crossentropy', metrics=['accuracy']) #model.compile(optimizer=RMSprop(lr=0.05,epsilon=1.0,decay=0.5), loss='categorical_crossentropy', metrics=['accuracy'])
def train_from_features(config): """ ConvNet as fixed feature extractor - Using the bottleneck features of a pre-trained network """ # Load the bottleneck features and the corresponding labels train_data = np.load( os.path.join(TEMP_DIR, config.features_dir + '/train_data.npy')) train_labels = np.load( os.path.join(TEMP_DIR, config.features_dir + '/train_labels.npy')) val_data = np.load( os.path.join(TEMP_DIR, config.features_dir + '/val_data.npy')) val_labels = np.load( os.path.join(TEMP_DIR, config.features_dir + '/val_labels.npy')) # Train a small FC model from scratch if config.load_model is not None: top_model = load_model(os.path.join(TEMP_DIR, config.load_model)) else: input = Input(shape=train_data.shape[1:]) output = Dense(len(set(train_labels)), activation='softmax')(input) top_model = Model(inputs=input, outputs=output) # Callbacks # Checkpointing the best model & restoring that as our model for prediction cb_checkpointer = ModelCheckpoint(filepath=os.path.join( WORK_DIR, 'model_fc.hdf5'), monitor='val_loss', save_best_only=True, mode='auto') cb_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=1) # Saving logs for analysis in TensorBoard cb_tensorboard = TensorBoard(log_dir=os.path.join(WORK_DIR, 'logs'), histogram_freq=0, write_graph=True) # One-hot encoding train_classes = to_categorical(train_labels, dtype='int32') val_classes = to_categorical(val_labels, dtype='int32') # Fit the model to data top_model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(), metrics=['accuracy']) fit_history = top_model.fit( train_data, train_classes, validation_data=(val_data, val_classes), epochs=config.epochs, callbacks=[cb_checkpointer, cb_lr, cb_tensorboard]) # Save the full model to the disk if config.save_full: base_model = InceptionV3(include_top=False, pooling='avg') top_model = load_model(os.path.join(WORK_DIR, 'model_fc.hdf5')) model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) model.save(os.path.join(WORK_DIR, 'model.hdf5')) os.remove(os.path.join(WORK_DIR, 'model_fc.hdf5')) # Save the metrics history to the disk with open(os.path.join(WORK_DIR, 'fit_history.history'), 'wb') as f: pickle.dump(fit_history.history, f) # Save normalized confusion matrix for later use in penalization if config.save_conf_weights: pred = model.predict(val_data) conf_weights = confusion_matrix(val_labels, pred.argmax(axis=1)) np.fill_diagonal(conf_weights, 0) conf_weights = 1 + conf_weights / np.max(conf_weights) np.save(os.path.join(WORK_DIR, 'conf_weights.npy'), conf_weights) # Rename dir to contain loss info best_loss = np.round(np.min(fit_history.history['val_loss']), 4) NEW_DIR = os.path.join(TEMP_DIR, 'loss_' + str(best_loss)) if os.path.isdir(NEW_DIR): shutil.rmtree(NEW_DIR) os.rename(WORK_DIR, NEW_DIR) print("Best model:", NEW_DIR)
Args: base_model: keras model excluding top nb_classes: # of classes Returns: new keras model with last layer """ x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(FC_SIZE, activation='relu')(x) #new FC layer, random init predictions = Dense(nb_classes, activation='softmax')(x) #new softmax layer model = Model(input=base_model.input, output=predictions) return model model = InceptionV3() add_new_last_layer(model, 47) model = model.load_weights( "D:\\ETUDES\\3A\\OSY\\Deep_learning\\projet\\dp_logo\\inceptionv3ft.model") target_size = (299, 299) def predict(model, img, target_size, top_n=3): """Run model prediction on image Args: model: keras model img: PIL format image target_size: (w,h) tuple top_n: # of top predictions to return Returns: list of predicted labels and their probabilities
def Network_config(class_num=4, epoch=200, initial_epoch=0, batch_size=32, train_data=None, train_label=None, test_data=None, test_label=None, fold=0): adam = Adam(lr=0.005, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.000) sgd = SGD(lr=0.001, momentum=0.9, decay=0.0, nesterov=False) K.set_learning_phase(1) base_model = InceptionV3(input_tensor=Input(shape=(299, 299, 3)), weights='imagenet', include_top=False) x = base_model.output # K.set_learning_phase(1) x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) x = BatchNormalization()(x) x = Dense(512, activation='relu')(x) x = BatchNormalization()(x) predictions = Dense(class_num, activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) for layer in (base_model.layers): layer.trainable = False if layer.name.startswith('bn') or 'bn' in layer.name: layer.call(layer.input, training=False) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=[keras.metrics.categorical_accuracy]) tools.create_directory('./tmpinception/') weights_file = './tmpinception/' + str(fold)+'-weights.{epoch:02d}-{categorical_accuracy:.4f}-{val_loss:.4f}-{val_categorical_accuracy:.4f}.h5' csv_file = './tmpinception/record.csv' lr_reducer = ReduceLROnPlateau(monitor='categorical_accuracy', factor=0.5, cooldown=0, patience=5, min_lr=0.5e-6) early_stopper = EarlyStopping(monitor='val_categorical_accuracy', min_delta=1e-4, patience=50) model_checkpoint = ModelCheckpoint(weights_file, monitor='val_categorical_accuracy', save_best_only=True, verbose=2, save_weights_only=True, mode='max') tensorboard = TensorBoard(log_dir='./logs/', histogram_freq=0, batch_size=8, write_graph=True, write_grads=True, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None) CSV_record = CSVLogger(csv_file, separator=',', append=True) callbacks = [lr_reducer, early_stopper, model_checkpoint, tensorboard, CSV_record] gc.disable() model.fit_generator( generator=tools.batch_generator(np.array(train_data), np.array(train_label), batch_size, True, class_num), steps_per_epoch=int(len(train_label)/batch_size)-1, max_q_size=20, initial_epoch=initial_epoch, epochs=epoch, verbose=1, callbacks=callbacks, validation_data=tools.batch_generator(np.array(test_data), np.array(test_label), batch_size, True, class_num), validation_steps=int(len(test_label)/batch_size)-1, class_weight='auto') all_y_pred = [] all_y_true = [] for test_data_batch, test_label_batch in tools.batch_generator_confusion_matrix(np.array(test_data),np.array(test_label), batch_size, True, class_num): y_pred = model.predict(test_data_batch, batch_size) y_true = test_label_batch for y_p in y_pred: all_y_pred.append(np.where(y_p == max(y_p))[0][0]) for y_t in y_true: all_y_true.append(np.where(y_t == max(y_t))[0][0]) confusion = confusion_matrix(y_true=all_y_true,y_pred=all_y_pred) print(confusion) f = open('confusion_matrix.txt','a+') f.write(str(all_y_true)+"\n") f.write(str(all_y_pred)+"\n") f.write(str(confusion)+'\n') f.close() gc.enable()
def create_model(self, model_type='xception', load_weights=None): if (model_type == 'inceptionv3' or model_type == 1): base = InceptionV3(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'inceptionv3' pred = base.output elif (model_type == 'resnet50' or model_type == 2): base = ResNet50(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'resnet50' pred = base.output elif (model_type == 'vgg19' or model_type == 3): base = VGG19(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'vgg19' pred = base.output elif (model_type == 'vgg16' or model_type == 4): base = VGG16(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'vgg16' pred = base.output elif (model_type == 'resnet152' or model_type == 5): sys.path.append(os.path.join(PATH, "resnet", "keras-resnet")) from resnet import ResnetBuilder resbuild = ResnetBuilder() base = resbuild.build_resnet_152(self.input_shape, self.output_size) model_name = 'resnet152' pred = base.output elif (model_type == 'resnet50MOD' or model_type == 6): sys.path.append(os.path.join(PATH, "resnet", "keras-resnet")) from resnet import ResnetBuilder resbuild = ResnetBuilder() base = resbuild.build_resnet_50(self.input_shape, self.output_size) model_name = 'resnet50MOD' pred = base.output elif (model_type == 'inceptionv3MOD' or model_type == 7): from keras.applications.inception_v3_mod import InceptionV3MOD base = InceptionV3MOD(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'inceptionv3MOD' pred = base.output else: base = Xception(include_top=False, weights='imagenet', input_tensor=self.input_tensor, classes=self.output_size, pooling='avg') model_name = 'xception' pred = base.output pred = Dense(self.output_size, activation='sigmoid', name='predictions')(pred) self.model = Model(base.input, pred, name=model_name) if load_weights != None: self.model.load_weights(load_weights) for layer in base.layers: layer.trainable = True self.compile()
target_size=(img_height, img_width), batch_size=n_batch_size, shuffle=True, subset='training') vali_generator = train_datagen.flow_from_directory('./train', target_size=(img_height, img_width), batch_size=n_batch_size, shuffle=True, subset='validation') # 以訓練好的 Xception 為基礎來建立模型 net = InceptionV3(input_shape=(img_height, img_width, 3), include_top=False, weights='imagenet', pooling='max') # 增加 Dense layer 與 softmax 產生個類別的機率值 x = net.output x = Dense(2048, activation='relu')(x) x = Dropout(0.5)(x) x = Dense(256, activation='relu')(x) output_layer = Dense(6, activation='softmax', name='softmax')(x) # 設定要進行訓練的網路層 model = Model(inputs=net.input, outputs=output_layer) # 取ImageNet中的起始Weight,不使他隨機產生,故凍結最底層 FREEZE_LAYERS = 1 for layer in model.layers[:FREEZE_LAYERS]:
features = [] i = 0 data_directory = '/home/ubantu/Desktop/DL_A3 Data/classification task/my_class_data/' directories = [ d for d in os.listdir(data_directory) if os.path.isdir(os.path.join(data_directory, d)) ] print(directories) for d in directories: for image_file in glob.iglob(data_directory + d + '/*.jpg'): print(i) i += 1 base_model = InceptionV3(weights='imagenet') model = Model(inputs=base_model.input, outputs=base_model.layers[-1].output) # for i in range(images) # chotadata = '/home/ubantu/Desktop/DL_A3 Data/chota_data/' # for d in directories: # directories = [d for d in os.listdir(chotadata) # if os.path.isdir(os.path.join(chotadata, d))] # i = 0 # for image_file in glob.iglob(d + '/*.jpg'): # print(i) # i += 1
def train(args): """Use transfer learning and fine-tuning to train a network on a new dataset""" nb_train_samples = get_nb_files(args.train_dir) nb_classes = len(glob.glob(args.train_dir + "/*")) # nb_val_samples = get_nb_files(args.val_dir) nb_epoch = int(args.nb_epoch) batch_size = int(args.batch_size) # data prep train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, rotation_range=15, # width_shift_range=0.2, # height_shift_range=0.2, # shear_range=0.2, # zoom_range=0.2, horizontal_flip=True ) test_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, rotation_range=15, # width_shift_range=0.2, # height_shift_range=0.2, # shear_range=0.2, # zoom_range=0.2, horizontal_flip=True ) train_generator = train_datagen.flow_from_directory( args.train_dir, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, ) validation_generator = test_datagen.flow_from_directory( args.val_dir, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, ) # setup model # include_top=False excludes final FC layer base_model = InceptionV3(weights='imagenet', include_top=False) model = add_new_last_layer(base_model, nb_classes) # transfer learning setup_to_transfer_learn(model, base_model) history_tl = model.fit_generator( train_generator, epochs=nb_epoch, steps_per_epoch=nb_train_samples // batch_size, validation_data=validation_generator, validation_steps=nb_train_samples // batch_size, class_weight='auto') if not args.no_plot: plot_training(history_tl) # fine-tuning setup_to_finetune(model) history_ft = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=nb_epoch, validation_data=validation_generator, validation_steps=nb_train_samples // batch_size, class_weight='auto') model.save(args.output_model_file) if not args.no_plot: plot_training(history_ft)
# -*- coding: utf-8 -*- from keras.applications.inception_v3 import InceptionV3 from keras.layers import Input # this could also be the output a different Keras model or layer input_tensor = Input( shape=(224, 224, 3)) # this assumes K.image_data_format() == 'channels_last' model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)
10, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0.125, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.125, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False, # randomly flip images # rescale=1. / 255, fill_mode='nearest') datagen.fit(X_train) # generator = datagen.flow(X_train, Y_train, batch_size=32) # val_generator = datagen.flow(X_test, Y_test, batch_size=32) base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=Input(shape=(96, 96, 3))) x = base_model.output x = GlobalAveragePooling2D()(x) # # x = Flatten()(x) x = Dense(4096)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(.5)(x) predictions = Dense(sum_laber, activation='softmax')(x) model = Model(input=base_model.input, output=predictions) # model = VGG16() for layer in model.layers[:172]: layer.trainable = False