def getInceptionV3Architecture(classes, dropoutRate): # create the base pre-trained model base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) # InceptionV3 x = base_model.output x = GlobalAveragePooling2D()(x) # let's add a fully-connected layer x = Dense(1024, activation='relu', kernel_initializer='random_uniform', bias_initializer='random_uniform', bias_regularizer=regularizers.l2(0.01))(x) # add Dropout regularizer x = Dropout(dropoutRate)(x) # and a logistic layer with all car classes predictions = Dense(len(classes), activation='softmax', kernel_initializer='random_uniform', bias_initializer='random_uniform', bias_regularizer=regularizers.l2(0.01), name='predictions')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) for layer in enumerate(base_model.layers): layer[1].trainable = False return model
def model_inception_pooled(input_shape=(None, None, 3), indexes=list(range(11)), pool_size=(5, 5), name='', return_sizes=False): """ Returns the wide MLSP features, spatially pooled, from InceptionV3. Similar to `model_inception_multigap`. * input_shape: shape of the input images * indexes: indices to use from the usual pools * pool_size: spatial extend of the MLSP features * name: name of the model * return_sizes: return the sizes of each layer: (model, pool_sizes) :return: model or (model, pool_sizes) """ print('Loading InceptionV3 multi-pooled with input_shape:', input_shape) model_base = InceptionV3(weights = 'imagenet', include_top = False, input_shape = input_shape) print('Creating multi-pooled model') ImageResizer = Lambda(lambda x: tf.image.resize_area(x, pool_size), name='feature_resizer') feature_layers = [model_base.get_layer('mixed%d' % i) for i in indexes] pools = [ImageResizer(l.output) for l in feature_layers] conc_pools = Concatenate(name='conc_pools', axis=3)(pools) model = Model(inputs = model_base.input, outputs = conc_pools) if name: model.name = name if return_sizes: pool_sizes = [[np.int32(x) for x in f.get_shape()[1:]] for f in pools] return model, pool_sizes else: return model
def merge_resnet_inceptionv3flat(NUM_CLASS): inceptionv3 = InceptionV3(weights='imagenet', include_top=False) resnet50 = ResNet50(weights='imagenet', include_top=False) res_out = resnet50.output res_out = GlobalAveragePooling2D()(res_out) res_out = Dropout(0.5)(res_out) res_out = Dense(2048)(res_out) inc_out = inceptionv3.output inc_out = GlobalAveragePooling2D()(inc_out) inc_out = Dropout(0.5)(inc_out) inc_out = Dense(2048)(inc_out) i_flat = Flatten()(inc_out) r_flat = Flatten()(res_out) merge = Concatenate()([r_flat, i_flat]) x = Dense(2048)(merge) x = Dropout(0.5)(x) x = Dense(1024)(x) x = Dense(512)(x) x = Dropout(0.5)(x) output = Dense(NUM_CLASS, activation='softmax', name='output_layer')(x) model = Model(inputs=[resnet50.input, inceptionv3.input], outputs=output) # plot graph plot_model(model, to_file='multiple_inputs.png', show_shapes=True, dpi=600, expand_nested=False) # model.summary() return model
def load_extractor_model(model_name="InceptionV3", flavor=1): """Load variant of InceptionV3 or VGG16 model specified. Args: model_name: string, either InceptionV3 or VGG16 flavor: int specifying the model variant and input_shape. For InceptionV3, the map is {0: default, 1: 200*200, truncate last Inception block, 2: 200*200, truncate last 2 blocks, 3: 200*200, truncate last 3 blocks, 4: 200*200} For VGG16, it only changes the input size, {0: 224 (default), 1: 128, 2: 64}. """ start = timer() if model_name == "InceptionV3": from tensorflow.keras.applications.inception_v3 import InceptionV3 from tensorflow.keras.applications.inception_v3 import preprocess_input model = InceptionV3(weights="imagenet", include_top=False) trunc_layer = [-1, 279, 248, 228, -1] i_layer = flavor model_out = Model( inputs=model.inputs, outputs=model.layers[trunc_layer[i_layer]].output ) input_shape = (299, 299, 3) if flavor == 0 else (200, 200, 3) elif model_name == "VGG16": from tensorflow.keras.applications.vgg16 import VGG16 from tensorflow.keras.applications.vgg16 import preprocess_input model_out = VGG16(weights="imagenet", include_top=False) input_length = [224, 128, 64][flavor] input_shape = (input_length, input_length, 3) end = timer() print("Loaded {} feature extractor in {:.2f}sec".format(model_name, end - start)) return model_out, preprocess_input, input_shape
def test_anchor_images(): os.environ.clear() alibi_model = os.path.join( kfserving.Storage.download(IMAGENET_EXPLAINER_URI), EXPLAINER_FILENAME) with open(alibi_model, "rb") as f: model = InceptionV3(weights="imagenet") predictor = lambda x: model.predict(x) # pylint:disable=unnecessary-lambda alibi_model = dill.load(f) anchor_images = AnchorImages(predictor, alibi_model, batch_size=25, stop_on_first=True) category = "Persian cat" image_shape = (299, 299, 3) data, _ = fetch_imagenet(category, nb_images=10, target_size=image_shape[:2], seed=2, return_X_y=True) images = preprocess_input(data) print(images.shape) np.random.seed(0) explanation = anchor_images.explain(images[0:1]) exp_json = json.loads(explanation.to_json()) assert exp_json["data"]["precision"] > 0.9
def calculate_inception_score(images, n_split=10, eps=1E-16): # load inception v3 model model = InceptionV3() # convert from uint8 to float32 processed = images.astype('float32') # pre-process raw images for inception v3 model processed = preprocess_input(processed) # predict class probabilities for images yhat = model.predict(processed) # enumerate splits of images/predictions scores = list() n_part = floor(images.shape[0] / n_split) for i in range(n_split): # retrieve p(y|x) ix_start, ix_end = i * n_part, i * n_part + n_part p_yx = yhat[ix_start:ix_end] # calculate p(y) p_y = expand_dims(p_yx.mean(axis=0), 0) # calculate KL divergence using log probabilities kl_d = p_yx * (log(p_yx + eps) - log(p_y + eps)) # sum over classes sum_kl_d = kl_d.sum(axis=1) # average over images avg_kl_d = mean(sum_kl_d) # undo the log is_score = exp(avg_kl_d) # store scores.append(is_score) # average across images is_avg, is_std = mean(scores), std(scores) return is_avg, is_std
def get_model(): base_model = InceptionV3(weights='imagenet',include_top=False) base_model.trainable =False y = base_model.output y = GlobalAveragePooling2D()(y) y = Dense(133, activation='softmax')(y) return Model(inputs=base_model.input,outputs=y)
def calculate_inception_score(images, n_split=5, eps=1E-16): # load inception v3 model model = InceptionV3() # predict class probabilities for images yhat = model.predict(images) # enumerate splits of images/predictions scores = list() # calculate the score in batches n_part = floor(images.shape[0] / n_split) for i in range(n_split): # retrieve p(y|x) these are [0, 0.5, 0, 0.5], etc. ix_start, ix_end = i * n_part, i * n_part + n_part p_yx = yhat[ix_start:ix_end] # calculate p(y) p_y = expand_dims(p_yx.mean(axis=0), 0) # calculate KL divergence using log probabilities kl_d = p_yx * (log(p_yx + eps) - log(p_y + eps)) # sum over classes sum_kl_d = kl_d.sum(axis=1) # average over images avg_kl_d = mean(sum_kl_d) # undo the log is_score = exp(avg_kl_d) # store scores.append(is_score) # average across images is_avg, is_std = mean(scores), std(scores) return is_avg, is_std
def __init__(self, gt, gen): from tensorflow.keras.applications.inception_v3 import InceptionV3 self.gt = gt self.gen = gen self.model = InceptionV3( include_top=False, pooling='avg', input_shape=(299, 299, 3)) # prepare the inception v3 model
def loadPretrainedWeights(): pretrained_weights={} pretrained_weights['vgg16']=VGG16(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['vgg19']=VGG19(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['resnet50']=ResNet50(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inceptionv3']=InceptionV3(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['inception-resentv2']=InceptionResNetV2(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['xception']=Xception(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet121']=DenseNet121(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet169']=DenseNet169(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['densenet201']=DenseNet201(weights='imagenet', include_top=False,pooling='avg') pretrained_weights['mobilenet']=MobileNet(weights='imagenet', include_top=False,pooling='avg') #N retrained_weights['nasnetlarge']=NASNetLarge(weights='imagenet', include_top=False,pooling='avg',input_shape = (224, 224, 3)) #N pretrained_weights['nasnetmobile']=NASNetMobile(weights='imagenet', include_top=False,pooling='avg') #N pretrained_weights['mobilenetV2']=MobileNetV2(weights='imagenet', include_top=False,pooling='avg') return pretrained_weights
def extractImgFeature(self, filename, modelType): if modelType == 'inceptionv3': from tensorflow.keras.applications.inception_v3 import preprocess_input target_size = (299, 299) model = InceptionV3() elif modelType == 'xception': from tensorflow.keras.applications.xception import preprocess_input target_size = (299, 299) model = Xception() elif modelType == 'vgg16': from tensorflow.keras.applications.vgg16 import preprocess_input target_size = (224, 224) model = VGG16() elif modelType == 'resnet50': from tensorflow.keras.applications.resnet50 import preprocess_input target_size = (224, 224) model = ResNet50() model.layers.pop() model = Model(inputs=model.inputs, outputs=model.layers[-1].output) image = load_img(filename, target_size=target_size) # Loading and resizing image image = img_to_array( image) # Convert the image pixels to a numpy array image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # Reshape data for the model image = preprocess_input( image) # Prepare the image for the CNN Model model features = model.predict( image, verbose=0) # Pass image into model to get encoded features return features
def preprocessing_manager(cfg): if cfg["dataset"]["encoding"] == "inception": img_model = InceptionV3(weights='imagenet') dataset_preprocessor = PreProcessing(cfg, "inception", (299, 299)) dataset_preprocessor.run_one_time_encoding(img_model) # Load train, validation sets from the pre-processor return dataset_preprocessor.get_encoding_keras_generators("inception") elif cfg["dataset"]["encoding"] == "resnet50": img_model = ResNet50(weights='imagenet') dataset_preprocessor = PreProcessing(cfg, "resnet50", (224, 224)) dataset_preprocessor.run_one_time_encoding(img_model) # Load train, validation sets from the pre-processor return dataset_preprocessor.get_encoding_keras_generators("resnet50") elif cfg["dataset"]["encoding"] == "simple": dataset_preprocessor = PreProcessing(cfg, None, (299, 299)) return dataset_preprocessor.get_image_keras_generators() else: RuntimeError("Unsupported encoding type!")
def pre_trained_softmax(d): # ''' # param: d - d['list1'] = ['ResNet50', 'VGG16', 'VGG19', 'InceptionV3', 'Xception'] # - d['num_classes'] = num_classes # - d['img_size'] = img_size # - d['channels'] = channels # returns: pre_trained designed model # ''' model = Sequential() img_size = d['img_size'] channels = d['channels'] num_classes = d['num_classes'] input_shape = (img_size, img_size, channels) for i, pre_trained in enumerate(d['list1']): if pre_trained == 'ResNet50': # Working base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'VGG16': base_model = VGG16(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'VGG19': base_model = VGG19(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'InceptionV3': base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) elif pre_trained == 'Xception': base_model = Xception(include_top=False, weights='imagenet', input_shape=(img_size, img_size, channels)) x = Flatten()(base_model.output) top_model = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=top_model) return model
def get_weights(save_dir: Path, model_name: str, dtype: str) -> str: """Download pre-trained imagenet weights for model. Args: save_dir: Path to where checkpoint must be downloaded. model_name: Type of image classification model, must be one of ("GoogleNet", "InceptionV1", "MobileNet", "MobileNetV2", "NASNetMobile", "DenseNet121", "ResNet50", "Xception", "InceptionV3") in all lower case. dtype: Data type of the network. Returns: Path to checkpoint file. """ if isinstance(save_dir, str): save_dir = Path(save_dir) g = tf.Graph() with tf.Session(graph=g) as sess: keras_backend.set_floatx(dtype) keras_backend.set_session(sess) if model_name == "mobilenet": MobileNet(weights='imagenet') saver = tf.train.Saver() elif model_name == "mobilenetv2": MobileNetV2(weights='imagenet') saver = tf.train.Saver() elif model_name == "nasnetmobile": NASNetMobile(weights='imagenet') saver = tf.train.Saver() elif model_name == "densenet121": DenseNet121(weights='imagenet') saver = tf.train.Saver() elif model_name == "resnet50": ResNet50(weights='imagenet') saver = tf.train.Saver() elif model_name == "xception": Xception(weights='imagenet') saver = tf.train.Saver() elif model_name == "inceptionv3": InceptionV3(weights='imagenet') saver = tf.train.Saver() elif model_name in ("googleNet", "inceptionv1"): tar_file = get_file( fname='inceptionv1_tar.gz', origin= 'http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz' ) tar_file_reader = tarfile.open(tar_file) tar_file_reader.extractall(save_dir) if dtype == 'float16': saver = convert_ckpt_to_fp16( Path(save_dir, 'inception_v1.ckpt').as_posix()) sess.run(tf.global_variables_initializer()) else: raise ValueError("""Requested model type = %s not one of ["GoogleNet", "InceptionV1", "MobileNet", "MobileNetV2", "NASNetMobile", "DenseNet121", "ResNet50", "Xception", "InceptionV3"].""" % model_name) save_dir.mkdir(parents=True, exist_ok=True) return saver.save(sess, Path(save_dir, f"{model_name}.ckpt").as_posix())
def __init__(self): ''' Initializes the class. ''' self.model = InceptionV3(include_top=False, weights='imagenet', input_tensor=None, input_shape=(256, 256, 3), pooling='avg', classes=1000)
def load_transfer_model(lr, classes=1, optimizer=RMSprop, target_size: Tuple[int, int] = (150, 150)): base_model = InceptionV3(input_shape=(*target_size, 3), include_top=False, weights='imagenet')
def inceptionv3(): base_model = InceptionV3(include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=x) model.compile(optimizer='adam', loss='binary_crossentropy') return model
def model_training(train_x, val_x, train_y, val_y, epochs = 20, fine_tune_at = 251): train_dataset = tf.data.Dataset.from_tensor_slices((train_x/255.0, train_y)) val_dataset = tf.data.Dataset.from_tensor_slices((val_x/255.0, val_y)) BATCH_SIZE = 16 SHUFFLE_BUFFER_SIZE = 100 # Shuffle and batch the datasets train_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE) val_dataset = val_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE) #Configure the dataset for performance AUTOTUNE = tf.data.experimental.AUTOTUNE train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE) val_dataset = val_dataset.prefetch(buffer_size=AUTOTUNE) #create model IMG_SHAPE = (229, 229, 3) base_model = InceptionV3(input_shape=IMG_SHAPE, include_top=False, weights='imagenet') base_model.trainable = False image_batch, label_batch = next(iter(train_dataset)) feature_batch = base_model(image_batch) #transfer learning with fine-tuning (start directly with fine-tuning epoch) base_model.trainable = True # Freeze all the layers before the `fine_tune_at` layer for layer in base_model.layers[:fine_tune_at]: layer.trainable = False #add top layer global_average_layer = tf.keras.layers.GlobalAveragePooling2D() feature_batch_average = global_average_layer(feature_batch) prediction_layer = tf.keras.layers.Dense(1) prediction_batch = prediction_layer(feature_batch_average) #data augmentation data_augmentation = tf.keras.Sequential([ tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')]) #define model inputs = tf.keras.Input(shape=(229, 229, 3)) x = data_augmentation(inputs) x = base_model(x, training=False) x = global_average_layer(x) x = tf.keras.layers.Dropout(0.2)(x) outputs = prediction_layer(x) model = tf.keras.Model(inputs, outputs) #compile the model base_learning_rate = 0.0001 model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate/10), loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy']) #fit the model history = model.fit(train_dataset, epochs=epochs, validation_data=val_dataset) return model, history
def get_model(args): logging.info('get model') # Get the InceptionV3 model so we can do transfer learning if args.base_modelname == 'inceptionV3': base_model = InceptionV3(weights='imagenet', include_top = False, input_shape=(299, 299, 3)) out = base_model.output out = Flatten()(out) # out = GlobalAveragePooling2D()(out) out = Dense(512, activation='relu')(out) out = Dense(512, activation='relu')(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=base_model.input, outputs=predictions) elif args.base_modelname == 'inceptionResNetV2': base_model = InceptionResNetV2(weights='imagenet', include_top = False, input_shape=(args.IMG_WIDTH,args.IMG_WIDTH,3)) out = base_model.output out = GlobalAveragePooling2D()(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=base_model.input, outputs=predictions) elif args.base_modelname == 'MobileNetV2': base_model = MobileNetV2(weights='imagenet', include_top = False, input_shape=(args.IMG_WIDTH,args.IMG_WIDTH,3)) out = base_model.output out = GlobalAveragePooling2D()(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) out = Dense(512, activation='relu')(out) out = Dropout(0.5)(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=base_model.input, outputs=predictions) else: x= Input(shape=(args.IMG_WIDTH,args.IMG_WIDTH,3)) out = GlobalAveragePooling2D()(x) out = Dense(128, activation='relu')(out) out = Dropout(0.5)(out) total_classes = train_generator.num_classes predictions = Dense(total_classes, activation='softmax')(out) model = Model(inputs=x, outputs=predictions) model.compile(Adam(lr = .0001), loss = 'categorical_crossentropy', metrics = ['accuracy']) # model.summary() return model
def main(run_config): bottleneck_executor = BottleneckExecutor( image_dir=run_config['image_dir'], tfhub_module_url= 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/1', compressed_bottleneck_file_path=run_config['bottleneck_path']) bottlenecks = bottleneck_executor.get_bottlenecks() image_count = bottlenecks.shape[0] num_classes = len(bottlenecks['class'].unique()) all_image_paths = bottlenecks['path'].values all_image_labels = bottlenecks['class'].values label_to_index = dict( (name, index) for index, name in enumerate(bottlenecks['class'].unique())) all_image_labels_one_hot = [ label_to_index[label] for label in all_image_labels ] path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths) image_ds = path_ds.map(load_and_preprocess_image, num_parallel_calls=AUTOTUNE) label_ds = tf.data.Dataset.from_tensor_slices( tf.cast(all_image_labels_one_hot, tf.int64)) image_label_ds = tf.data.Dataset.zip((image_ds, label_ds)) steps_per_epoch = math.ceil(len(all_image_paths) / BATCH_SIZE) ds = image_label_ds.cache() ds = ds.apply( tf.data.experimental.shuffle_and_repeat(buffer_size=image_count)) ds = ds.batch(BATCH_SIZE).prefetch(buffer_size=AUTOTUNE) # ds = ds.batch(BATCH_SIZE) # time_shuffle_and_repeat(ds, batches=2*steps_per_epoch+1) ''' Fixed-Feature Extractor InceptionV3 with Transfer Learning:''' base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(299, 299, 3)) # 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) # and a logistic layer -- let's say we have 200 classes predictions = Dense(num_classes, activation='softmax')(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 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='sparse_categorical_crossentropy') # train the model on the new data for a few epochs model.fit(ds, epochs=3, steps_per_epoch=steps_per_epoch) # return the loss value and metric values for the model in test mode: print(model.evaluate(ds, batch_size=BATCH_SIZE, steps=None))
def encode_images(image): img_model = InceptionV3( weights='imagenet') #Replace with Resnet if Resnet50 works better model = encoder_model(img_model) image = np.expand_dims(np.asarray(cv2.resize(image, (299, 299))) / 255.0, axis=0) enc_train = model.predict(image) print(enc_train.shape) return enc_train
def make_model(name, input_shape, num_classes): if name == "VGG19": from tensorflow.keras.applications.vgg19 import VGG19 base_model = VGG19(include_top=False, input_shape=input_shape, weights='imagenet', layers=tf.keras.layers, pooling="max") elif name == "ResNet50": from tensorflow.keras.applications.resnet50 import ResNet50 base_model = ResNet50(include_top=False, input_shape=input_shape, weights='imagenet', layers=tf.keras.layers, pooling="avg") elif name == "VGG16": from tensorflow.keras.applications.vgg16 import VGG16 base_model = VGG16(include_top=False, input_shape=input_shape, weights='imagenet', layers=tf.keras.layers, pooling="max") elif name == "InceptionV3": from tensorflow.keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(include_top=False, input_shape=input_shape, weights='imagenet', layers=tf.keras.layers, pooling="max") elif name == "MobileNet": from tensorflow.keras.applications.mobilenet import MobileNet base_model = MobileNet(include_top=False, input_shape=input_shape, weights='imagenet', layers=tf.keras.layers, pooling="max") elif name == "VGG161": return pretrained_model1() elif name == "InceptionResNetV2": return pretrained_model2() elif name == "DenseNet121": return pretrained_model3() # model = Sequential() # model.add(base_model) # model.add(layers.Flatten()) # model.add(layers.Dense(256, activation='relu', name="Dense1")) # model.add(layers.Dense(num_classes, activation='softmax', name="Dense2")) # print(model.summary()) model = base_model.output # model=layers.Flatten()(model) model = layers.Dense(256, activation='relu', name="Dense1")(model) model = layers.Dense(num_classes, activation='softmax', name="Dense2")(model) headmodel = Model(inputs=base_model.input, outputs=model) # print(headmodel.summary()) # base_model.trainable = False return headmodel
def training(): K.clear_session() img_width, img_height = 299, 299 train_data_dir = dest_train validation_data_dir = dest_test nb_train_samples = 75750 nb_validation_samples = 25250 batch_size = 16 train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') inception = InceptionV3(weights='imagenet', include_top=False) x = inception.output x = GlobalAveragePooling2D()(x) x = Dense(128, activation='relu')(x) x = Dropout(0.2)(x) predictions = Dense(n, kernel_regularizer=regularizers.l2(0.005), activation='softmax')(x) model = Model(inputs=inception.input, outputs=predictions) model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) # checkpointer = ModelCheckpoint(filepath='food101/best_model_101class.hdf5', verbose=1, save_best_only=True) csv_logger = CSVLogger('food101/history.log') history = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size, epochs=10, verbose=1, callbacks=[csv_logger]) model.save('model_trained_101class.hdf5') return model
def train_model(n_classes,num_epochs, nb_train_samples,nb_validation_samples): K.clear_session() img_width, img_height = 299, 299 train_data_dir = 'food-101/train_mini' validation_data_dir = 'food-101/test_mini' batch_size = 16 bestmodel_path = 'bestmodel_'+str(n_classes)+'class.hdf5' trainedmodel_path = 'trainedmodel_'+str(n_classes)+'class.hdf5' history_path = 'history_'+str(n_classes)+'.log' train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') inception = InceptionV3(weights='imagenet', include_top=False) x = inception.output x = GlobalAveragePooling2D()(x) x = Dense(128,activation='relu')(x) x = Dropout(0.2)(x) predictions = Dense(n_classes,kernel_regularizer=regularizers.l2(0.005), activation='softmax')(x) model = Model(inputs=inception.input, outputs=predictions) model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) checkpoint = ModelCheckpoint(filepath=bestmodel_path, verbose=1, save_best_only=True) csv_logger = CSVLogger(history_path) history = model.fit_generator(train_generator, steps_per_epoch = nb_train_samples // batch_size, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size, epochs=num_epochs, verbose=1, callbacks=[csv_logger, checkpoint]) model.save(trainedmodel_path) class_map = train_generator.class_indices return history, class_map
def my_model_fn(model_name='resnet50'): reg = regularizers.l2(0.01) if model_name == 'vgg16': conv_base = VGG16(weights='imagenet', include_top=False, input_shape=input_shape) conv_base.trainable = False elif model_name == 'resnet50': conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape) conv_base.trainable = False elif model_name == 'inception_v3': conv_base = InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape) conv_base.trainable = False elif model_name == 'alexnet': conv_base = AlexNet(input_shape=input_shape, reg=reg) else: raise Exception( "[Model Error]: Select a model that is defined in this code!") model = models.Sequential() model.add(conv_base) if model_name == 'inception_v3' or model_name == 'resnet50': model.add(layers.GlobalAveragePooling2D()) else: model.add(layers.Flatten()) model.add( layers.Dense(512, activation='relu', kernel_initializer='uniform', kernel_regularizer=reg)) model.add(layers.Dropout(0.5)) model.add( layers.Dense(512, activation='relu', kernel_initializer='uniform', kernel_regularizer=reg)) model.add(layers.Dropout(0.5)) model.add( layers.Dense(2, activation='softmax', kernel_initializer='uniform', kernel_regularizer=reg)) model.compile(loss='categorical_crossentropy', optimizer=optimizers.SGD(lr=LEARN_RATE, momentum=0.9), metrics=['accuracy']) model.summary() return model
def testFIDComputation(self): input = tf.random.uniform((5, 256, 256, 3)) model = InceptionV3(include_top=False, pooling='avg', input_shape=(256, 256, 3)) self_fid = eval.inception.calculate_frechet_distance( model, input, input) print(self_fid)
def testInceptionComputation(self): input = tf.random.uniform((5, 256, 256, 3)) model = InceptionV3(include_top=False, pooling='avg', input_shape=(256, 256, 3)) inception_scores = eval.inception.calculate_inception_score( model, input) print(inception_scores)
def __init__(self, optimizer=Adam(), loss=categorical_crossentropy, metrics=[top_k_categorical_accuracy, categorical_accuracy]): super().__init__(optimizer=optimizer, loss=loss, metrics=metrics, MODEL_NAME=INCEPTION_V3_NAME) self.imagenet = InceptionV3() self.imagenet.layers[-1].activation = tf.keras.activations.linear
def prepare(img_path='./YellowLabradorLooking_new.jpg'): img = image.load_img(img_path, target_size=(299, 299)) img = image.img_to_array(img) img = preprocess_input(img) img = img[tf.newaxis, ...] img = tf.Variable(img, dtype=tf.float32) model = InceptionV3(include_top=True, weights="imagenet") model.trainable = False return img, model
def define_classifier_architecture(architecture_name, image_size, weights, classifier_kwargs=None): if architecture_name == 'MobileNet': model = MobileNetV2(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'VGG16': model = VGG16(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'VGG19': model = VGG19(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'NASNetMobile': model = NASNetMobile(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'NASNetLarge': model = NASNetLarge(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'InceptionV3': model = InceptionV3(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'InceptionResNetV2': model = InceptionResNetV2(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'Resnet50': model = ResNet50(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) elif architecture_name == 'EfficientNetB0': model = EfficientNetB0(input_shape=image_size, include_top=False, weights=weights, **classifier_kwargs) else: raise ValueError( f"Classifier '{architecture_name}' is wrong or not implemented.") return model