示例#1
0
def calculate_mode_score(gen_images_path,
                         real_images_path,
                         batch_size=32,
                         splits=10):
    # Create an instance of InceptionV3
    model = InceptionResNetV2()

    # Load real images
    real_images = None
    for image_ in glob.glob(real_images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if real_images is None:
            real_images = loaded_image
        else:
            real_images = np.concatenate([real_images, loaded_image], axis=0)

    # Load generated images
    gen_images = None
    for image_ in glob.glob(gen_images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if gen_images is None:
            gen_images = loaded_image
        else:
            gen_images = np.concatenate([gen_images, loaded_image], axis=0)

    # Calculate number of batches for generated images
    gen_num_batches = (gen_images.shape[0] + batch_size - 1) // batch_size
    gen_images_probs = None
    # Use InceptionV3 to calculate probabilities of generated images
    for i in range(gen_num_batches):
        image_batch = gen_images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if gen_images_probs is None:
            gen_images_probs = prob
        else:
            gen_images_probs = np.concatenate([prob, gen_images_probs], axis=0)

    # Calculate number of batches for real images
    real_num_batches = (real_images.shape[0] + batch_size - 1) // batch_size
    real_images_probs = None
    # Use InceptionV3 to calculate probabilities of real images
    for i in range(real_num_batches):
        image_batch = real_images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if real_images_probs is None:
            real_images_probs = prob
        else:
            real_images_probs = np.concatenate([prob, real_images_probs],
                                               axis=0)

    # KL-Divergence: compute kl-divergence and mean of it
    num_gen_images = len(gen_images)
    split_scores = []

    for j in range(splits):
        gen_part = gen_images_probs[j * (num_gen_images // splits):(j + 1) *
                                    (num_gen_images // splits), :]
        real_part = real_images_probs[j * (num_gen_images // splits):(j + 1) *
                                      (num_gen_images // splits), :]
        gen_py = np.mean(gen_part, axis=0)
        real_py = np.mean(real_part, axis=0)
        scores = []
        for i in range(gen_part.shape[0]):
            scores.append(entropy(gen_part[i, :], gen_py))

        split_scores.append(np.exp(np.mean(scores) - entropy(gen_py, real_py)))

    final_mean = np.mean(split_scores)
    final_std = np.std(split_scores)

    return final_mean, final_std
NAME = f"{EPOCHS}-EPOCHS-{str(LR)[2:]}-LR-{timestr}"

#savepath
model_path = f'models/{timestr}'
if not os.path.exists(model_path):
    os.makedirs(model_path)

# dimensions of our images.
img_size = (300, 300)
in_shape = (300, 300, 3)
input = Input(shape=in_shape, name='image_input')

#model
base_model = InceptionResNetV2(include_top=False,
                               weights='imagenet',
                               input_tensor=None,
                               input_shape=in_shape,
                               pooling='avg')

top_model = Sequential()
top_model.add(
    Dense(1, input_shape=(base_model.output_shape[1:]), activation='sigmoid'))
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))

adam = Adam(lr=LR,
            beta_1=BETA_1,
            beta_2=BETA_2,
            epsilon=EPSILON,
            decay=0.0,
            amsgrad=False)
model.compile(optimizer='adam',
示例#3
0
from skimage.color import rgb2lab, lab2rgb, rgb2gray, gray2rgb
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from skimage.transform import resize
from skimage.io import imsave
from keras.applications.inception_resnet_v2 import preprocess_input
from keras.models import load_model
import numpy as np
import os
import tensorflow as tf
from keras.preprocessing.image import array_to_img, img_to_array, load_img
import resizing
#from sklearn.metrics import mean_squared_error

inception = InceptionResNetV2(weights=None, include_top=True)
inception.load_weights(
    'E:/OmarKhaled/Work/Year 4/GP/DownloadScript/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5'
)
inception.graph = tf.get_default_graph()

print('done')


def mse(imageA, imageB):
    err = np.sum((imageA.astype("float") - imageB.astype("float"))**2)
    err /= float(imageA.shape[0] * imageA.shape[1])
    return err


def create_inception_embedding(grayscaled_rgb):
    grayscaled_rgb_resized = []
    for i in grayscaled_rgb:
def main():

    print_debug(DEBUG)
    # for dataset in [train_image_dir, test_image_dir]:
    for dataset in [test_image_dir, train_image_dir]:
        print(
            '========================================================================'
        )
        print('INCEPTIONNET')
        print('PROCESSING', dataset)
        print(
            '========================================================================'
        )

        df = pd.DataFrame(columns=['item_id', 'mean', 'std'])

        print("Loading images from directory : ", dataset)
        imgs = Path(dataset).files('*.png')
        imgs += Path(dataset).files('*.jpg')
        imgs += Path(dataset).files('*.jpeg')

        N = len(imgs)
        i = 0
        # with tf.device("CPU:0"):
        with tf.device("/device:GPU:0"):
            print('>> init')
            base_model = InceptionResNetV2(input_shape=(None, None, 3),
                                           include_top=False,
                                           pooling='avg',
                                           weights=None)
            x = Dropout(0.75)(base_model.output)
            x = Dense(10, activation='softmax')(x)

            print('>> load weights')
            model = Model(base_model.input, x)
            model.load_weights('weights/inception_resnet_weights.h5')

            score_list = []

            df_temp = pd.DataFrame()
            if DEBUG: STEP = 3
            else: STEP = 1000
            if dataset == train_image_dir: todir = train_filename
            else: todir = test_filename

            for img_path in imgs:
                if i % STEP == 0:
                    end_step = time.time()
                    print('----------------------------')
                    print('{}/{}'.format(i, N))
                    if i > 0:
                        print('time elapse:', end_step - start_step)
                        df = pd.concat([df, df_temp], axis=0)
                        save_pickle(df, todir)
                        df_temp = pd.DataFrame()
                    start_step = time.time()
                if DEBUG: print("\n>> Evaluating : ", img_path)

                img = load_img(img_path, target_size=target_size)
                x = img_to_array(img)
                x = np.expand_dims(x, axis=0)

                x = preprocess_input(x)

                scores = model.predict(x, batch_size=1, verbose=0)[0]

                mean = mean_score(scores)
                std = std_score(scores)

                file_name = Path(img_path).name.lower()
                score_list.append((file_name, mean))

                if DEBUG: print("NIMA Score : %0.3f +- (%0.3f)" % (mean, std))

                filename_w_ext = os.path.basename(img_path)
                filename, file_extension = os.path.splitext(filename_w_ext)

                temp = pd.DataFrame({
                    'item_id': [filename],
                    'mean': [mean],
                    'std': [std]
                })
                if DEBUG: print(temp)

                df = pd.concat([df, temp], axis=0)

                i = i + 1

        df = pd.concat([df, df_temp], axis=0)
        df = df.reset_index(drop=True)
        print(df)
        save_pickle(df, todir)
示例#5
0
import numpy as np
import os
import random
import tensorflow as tf

# In[ ]:

# # Get images
X = []
for filename in os.listdir('Train/'):
    X.append(img_to_array(load_img('Train/' + filename)))
X = np.array(X, dtype=float)
Xtrain = 1.0 / 255 * X

#Load weights
inception = InceptionResNetV2(weights='imagenet', include_top=False)
inception.graph = tf.get_default_graph()

# In[ ]:


def conv_stack(data, filters, s):
    output = Conv2D(filters, (3, 3),
                    strides=s,
                    activation='relu',
                    padding='same')(data)
    output = BatchNormalization()(output)
    return output


embed_input = Input(shape=(
示例#6
0
from keras.applications.inception_resnet_v2 import InceptionResNetV2
#Load the ... model
###vgg_conv = irn2
irn2 = InceptionResNetV2(include_top=False,
                         weights='imagenet',
                         input_tensor=None,
                         input_shape=(299, 299, 3),
                         pooling=None,
                         classes=1000)
for layer in irn2.layers[:-18]:
    layer.trainable = False
#for layer in irn2.layers:
#   print(layer,layer.trainable)

from keras import models
from keras import layers
from keras import optimizers

model = models.Sequential()
model.add(irn2)

model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(3, activation='softmax'))
#model.summary()
train_dir = 'images_train'
valid_dir = 'images_valid'
from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1. / 255,
示例#7
0
                                      verbose=1,
                                      save_best_only=True)

    # now our model is ready for training .. lets do that
    my_model.fit(train_data,
                 train_targets,
                 validation_data=(valid_data, valid_targets),
                 epochs=60,
                 batch_size=200,
                 callbacks=[my_checkpointer],
                 verbose=1)


# Transfere Learning section
# preprocess_input method is imported above from keras.applications.inception_resnet_v2
train_imgs_preprocess = preprocess_input(train_tensors)
valid_imgs_preprocess = preprocess_input(valid_tensors)
test_imgs_preprocess = preprocess_input(test_tensors)
del train_tensors, valid_tensors, test_tensors

transfer_model = InceptionResNetV2(include_top=False)

train_data = transfer_model.predict(train_imgs_preprocess)
valid_data = transfer_model.predict(valid_imgs_preprocess)
test_data = transfer_model.predict(test_imgs_preprocess)
# memory optimization step
del train_imgs_preprocess, valid_imgs_preprocess, test_imgs_preprocess

my_model = model()
model_training()
示例#8
0
print('================= Building Model =================')
print('==================================================')

print('current time: %s' % str(datetime.now()))

# t_x: ndarray of images
# t_y: ndarray of labels (patient age)
t_x, t_y = next(train_gen_chest)  # gets the next batch from the data generator
# t_x has 'channels_last' data format (default of InceptionResNetV2)
# print(t_x.shape[1:])
in_layer = Input(t_x.shape[1:])  # instantiate a Keras tensor

conv_base_model = InceptionResNetV2(include_top=True,
                                    # use default InceptionResNetV2 img size -- otherwise we would not be able to define our own input size!
                                    weights='imagenet',
                                    input_tensor=None,
                                    # input_shape=t_x.shape[1:],
                                    # pooling=None,
                                    # classes=1000
                                    )
conv_base_model.trainable = False

features = conv_base_model(in_layer)

classifier = Sequential()
print(conv_base_model.output_shape[1:])
classifier.add(Dense(256, activation='relu', input_shape=conv_base_model.output_shape[1:]))
# classifier.add(Dropout(0.5))
classifier.add(Dense(1, activation='relu'))

out_layer = classifier(features)
示例#9
0
def _create_pretrained_model(config_dict, num_classes):
    #
    # extract relevant parts of configuration
    #
    num_dense_units_list = []
    base_model = config_dict['base_model']
    num_dense_layers = config_dict['num_dense_layers']
    for i in range(num_dense_layers):
        num_dense_units_list.append(config_dict['num_dense_units_' + str(i)])
    activation = config_dict['activation']
    optimizer = config_dict['optimizer']
    learning_rate = config_dict['learning_rate']

    #
    # load pre-trained model
    #
    if base_model == 'InceptionV3':
        pretrained_model = InceptionV3(weights='imagenet', include_top=False)  
    elif base_model == 'Xception':
        pretrained_model = Xception(weights='imagenet', include_top=False)
    elif base_model == 'ResNet50':
        pretrained_model = ResNet50(weights='imagenet', include_top=False)
    elif base_model == 'MobileNet':
        pretrained_model = MobileNet(weights='imagenet', input_shape=(224, 224,3), include_top=False)
    elif base_model == 'InceptionResNetV2':
        pretrained_model = InceptionResNetV2(weights='imagenet', include_top=False)
    else:
        print("invalid model: ", base_model)
    
    x = pretrained_model.output

    # for i, layer in enumerate(pretrained_model.layers):
    #    print(i, layer.name)    
    
    
    #
    # add fully connected layers
    #
    x = pretrained_model.output

    x = GlobalAveragePooling2D()(x)
    for i in range(num_dense_layers):
        x = Dense(num_dense_units_list[i], activation=activation)(x)
    predictions = Dense(num_classes, activation='softmax')(x)
    #
    # finish building combined model, lock parameters of pretrained part
    #
    model = Model(inputs=pretrained_model.input, outputs=predictions)
    for layer in pretrained_model.layers:
        layer.trainable = False
    if optimizer == 'SGD':
        opt = optimizers.SGD(lr=learning_rate)
    elif optimizer == 'Adam':
        opt = optimizers.Adam(lr=learning_rate)
    elif optimizer == 'RMSProp':
        opt = optimizers.RMSprop(lr=learning_rate)
    else:
        raise NotImplementedError("Unknown optimizer: {}".format(optimizer))
    # compile the model (should be done *after* setting layers to
    # non-trainable)
    # metrics='accuracy' causes the model to store and report accuracy (train
    # and validate)
    model.compile(optimizer=opt,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    
    model.name = base_model    # to identify model in "unfreeze_layers() and "train()" function
    # print("successfuly created model: ", model.name)    
    
    return model
            '../../input/petfinder-adoption-prediction/test_images/*.jpg'))
    image_files = train_image_files + test_image_files
    train_images = pd.DataFrame(image_files, columns=['image_filename'])
    train_images['PetID'] = train_images['image_filename'].apply(
        lambda x: x.split('/')[-1].split('-')[0])

with timer('densenet'):
    batch_size = 16
    pet_ids = train_images['PetID'].values
    img_pathes = train_images['image_filename'].values
    n_batches = len(pet_ids) // batch_size + 1

    inp = Input((256, 256, 3))
    backbone = InceptionResNetV2(
        input_tensor=inp,
        weights=
        '../../input/Inception-Resnet-V2/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5',
        include_top=False)
    x = backbone.output
    x = GlobalAveragePooling2D()(x)
    x = Lambda(lambda x: K.expand_dims(x, axis=-1))(x)
    x = AveragePooling1D(4)(x)
    out = Lambda(lambda x: x[:, :, 0])(x)
    m = Model(inp, out)
    m.summary()

    features = []
    for b in range(n_batches):
        start = b * batch_size
        end = (b + 1) * batch_size
        batch_pets = pet_ids[start:end]
示例#11
0
    def create_model():
        base_model = None
        if params["model"] == "InceptionV3":
            params["train_threshold"] = 249
            base_model = InceptionV3(weights='imagenet',
                                     include_top=False,
                                     input_tensor=None,
                                     input_shape=(params["img_width"],
                                                  params["img_height"], 3))
        elif params["model"] == "xception":
            params["train_threshold"] = 106
            base_model = Xception(weights='imagenet',
                                  include_top=False,
                                  input_tensor=None,
                                  input_shape=(params["img_width"],
                                               params["img_height"], 3))
        elif params["model"] == "InceptionResNetV2":
            params["train_threshold"] = 727
            base_model = InceptionResNetV2(weights='imagenet',
                                           include_top=False,
                                           input_tensor=None,
                                           input_shape=(params["img_width"],
                                                        params["img_height"],
                                                        3))
        elif params["model"] == "DenseNet121":
            params["train_threshold"] = 403
            base_model = DenseNet121(weights='imagenet',
                                     include_top=False,
                                     input_tensor=None,
                                     input_shape=(params["img_width"],
                                                  params["img_height"], 3))
        elif params["model"] == "DenseNet169":
            params["train_threshold"] = 571
            base_model = DenseNet169(weights='imagenet',
                                     include_top=False,
                                     input_tensor=None,
                                     input_shape=(params["img_width"],
                                                  params["img_height"], 3))
        elif params["model"] == "DenseNet201":
            params["train_threshold"] = 683
            base_model = DenseNet201(weights='imagenet',
                                     include_top=False,
                                     input_tensor=None,
                                     input_shape=(params["img_width"],
                                                  params["img_height"], 3))
        elif params["model"] == "ResNet50":
            params["train_threshold"] = 140
            base_model = ResNet50(weights='imagenet',
                                  include_top=False,
                                  input_tensor=None,
                                  pooling=None,
                                  input_shape=(params["img_width"],
                                               params["img_height"], 3))
        else:
            print("unknown model")

        count = 0
        modelx = base_model.output

        while count < params["dense_num"]:
            count += 1
            string = "dense" + str(count)

            if "pool" in params[string]:
                if params[string]["pool"] == "avg_poolx":
                    modelx = GlobalAveragePooling2D(
                        name=params[string]["pool"])(modelx)

            modelx = Dense(params[string]["num"],
                           activation=params[string]["activation"])(modelx)

            if "dropout" in params[string]:
                modelx = Dropout(params[string]["dropout"])(modelx)

        model = Model(inputs=base_model.input, output=modelx)

        for layer in base_model.layers:
            layer.trainable = False

        model.compile(loss=params["loss"],
                      optimizer=params["phase1_optimizer"],
                      metrics=params["metrics"])

        return model
示例#12
0
def inception(trained=False, third_phase_train_reps=third_phase_train_reps):
    global inception_model, new_inception_model, optimizer
    tensorboard = TensorBoard(log_dir=third_phase_folder + 'tb_logs',
                              batch_size=batch_size)
    start_lr = 0.00015
    end_lr = 0.00001
    step_lr = (end_lr - start_lr) / (third_phase_train_reps - 1)

    if not trained:
        # create the base pre-trained model
        input_tensor = Input(shape=input_shape)
        base_model = InceptionResNetV2(input_tensor=input_tensor,
                                       weights='imagenet',
                                       include_top=False)

        # add a global spatial average pooling layer
        x = base_model.output
        x = GlobalAveragePooling2D()(x)
        # add a fully-connected layer
        x = Dense(1024, activation='relu')(x)
        # add a logistic layer
        predictions = Dense(classes_num, activation='softmax')(x)

        # this is the model we will train
        inception_model = Model(inputs=base_model.input, outputs=predictions)
        inception_model.compile(optimizer=Adam(lr=0.0001),
                                loss='categorical_crossentropy',
                                metrics=['acc'])

        for layer in inception_model.layers:
            layer.trainable = True
            if isinstance(layer, keras.layers.convolutional.Conv2D):
                layer.kernel_regularizer = regularizers.l2(0.001)
                layer.activity_regularizer = regularizers.l1(0.001)

        # add dropout and regularizer to the penultimate Dense layer
        predictions = inception_model.layers[-1]
        dropout = Dropout(0.2)
        fc = inception_model.layers[-2]
        fc.kernel_regularizer = regularizers.l2(0.001)
        fc.activity_regularizer = regularizers.l1(0.001)

        x = dropout(fc.output)
        predictors = predictions(x)
        new_inception_model = Model(inputs=inception_model.input,
                                    outputs=predictors)

        optimizer = Adam(lr=0.1234)

        new_inception_model.compile(optimizer=optimizer,
                                    loss='categorical_crossentropy',
                                    metrics=['acc'])
    else:
        new_inception_model = load_model(data_folder +
                                         '3rd_phase_inception_model.h5')
        optimizer = Adam(lr=0.1234)
        new_inception_model.compile(optimizer=optimizer,
                                    loss='categorical_crossentropy',
                                    metrics=['acc'])

    if not os.path.exists(third_phase_folder):
        os.makedirs(third_phase_folder)

    for i in range(third_phase_train_reps):
        lr = start_lr + step_lr * i
        K.set_value(new_inception_model.optimizer.lr, lr)
        print(i, 'out of ', third_phase_train_reps, '\nlearning rate ',
              K.eval(new_inception_model.optimizer.lr))
        history = new_inception_model.fit_generator(
            train_img_class_gen,
            steps_per_epoch=steps_per_small_epoch,
            epochs=small_epochs,
            verbose=2,
            validation_data=val_img_class_gen,
            validation_steps=val_steps_per_small_epoch,
            workers=4,
            callbacks=[tensorboard])
        #         history = new_inception_model.fit_generator(train_img_class_gen,
        #                                                    steps_per_epoch=steps_per_small_epoch,
        #                                                    epochs=small_epochs, verbose=2,
        #                                                    validation_data=val_img_class_gen, validation_steps=val_steps_per_small_epoch,
        #                                                    workers=4, callbacks=[LosswiseKerasCallback(tag='keras inception model')])
        print("iteration", i)
        if i % saves_per_epoch == 0:
            print('{} epoch completed'.format(int(i / saves_per_epoch)))

        if i >= 5:
            ts = calendar.timegm(time.gmtime())
            new_inception_model.save(third_phase_folder + str(ts) +
                                     '_inception_model.h5')
            save_obj(history.history,
                     str(ts) + '_inception_history.h5',
                     folder=third_phase_folder)

    new_inception_model.save(data_folder + '3rd_phase_inception_model.h5')
示例#13
0
    def __init__(self, top_model):

        self.top_model = top_model

        if self.top_model == 'mobilenet':
            from keras.applications.mobilenet_v2 import MobileNetV2
            self.input_shape = (224, 224)
            self.inp = Input((self.input_shape[0], self.input_shape[1], 3))
            self.initial_model = MobileNetV2(include_top=False,
                                             input_shape=(self.input_shape[0],
                                                          self.input_shape[1],
                                                          3),
                                             input_tensor=self.inp,
                                             pooling='avg')

        elif self.top_model == 'inception':
            from keras.applications.inception_v3 import InceptionV3
            self.input_shape = (299, 299)
            self.inp = Input((self.input_shape[0], self.input_shape[1], 3))
            self.initial_model = InceptionV3(include_top=False,
                                             input_shape=(self.input_shape[0],
                                                          self.input_shape[1],
                                                          3),
                                             input_tensor=self.inp,
                                             pooling='avg')

        elif self.top_model == 'vgg':
            from keras.applications.vgg19 import VGG19
            self.input_shape = (224, 224)
            self.inp = Input((self.input_shape[0], self.input_shape[1], 3))
            self.initial_model = VGG19(include_top=False,
                                       input_shape=(self.input_shape[0],
                                                    self.input_shape[1], 3),
                                       input_tensor=self.inp,
                                       pooling='avg')

        elif self.top_model == 'xception':
            from keras.applications.xception import Xception
            self.input_shape = (299, 299)
            self.inp = Input((self.input_shape[0], self.input_shape[1], 3))
            self.initial_model = Xception(include_top=False,
                                          input_shape=(self.input_shape[0],
                                                       self.input_shape[1], 3),
                                          input_tensor=self.inp,
                                          pooling='avg')

        elif self.top_model == 'resnet':
            from keras.applications.resnet50 import ResNet50
            self.input_shape = (224, 224)
            self.inp = Input((self.input_shape[0], self.input_shape[1], 3))
            self.initial_model = ResNet50(include_top=False,
                                          input_shape=(self.input_shape[0],
                                                       self.input_shape[1], 3),
                                          input_tensor=self.inp,
                                          pooling='avg')

        elif self.top_model == 'inception-resnet':
            from keras.applications.inception_resnet_v2 import InceptionResNetV2
            self.input_shape = (299, 299)
            self.inp = Input((self.input_shape[0], self.input_shape[1], 3))
            self.initial_model = InceptionResNetV2(
                include_top=False,
                input_shape=(self.input_shape[0], self.input_shape[1], 3),
                input_tensor=self.inp,
                pooling='avg')

        else:
            raise Exception(
                "Values allowed for model parameter are - mobilenet, inception, vgg, xception, resnet and inception-resnet. Value passed was: {}"
                .format(self.top_model))
示例#14
0
def image2sim(input_images, prefix='image'):

    PATH = exp_config.get('data', 'path')
    IDENTICAL_T = eval(exp_config.get('predicate_image',
                                      'identical_threshold'))
    method = exp_config.get('predicate_image', 'method')
    embedding_iters = eval(exp_config.get('cosine_embedding', 'n_iter'))

    assert method in [
        'identical', 'vgg16', 'vgg19', 'xception', 'inception_resnet_v2',
        'vggface'
    ]
    print('input_images', len(input_images))

    if os.path.isfile(PATH + prefix + '_list' + '.txt'):
        images = list()
        fin = open(PATH + prefix + '_list' + '.txt', 'r')
        for line in fin:
            images.append(line[:-1])
        fin.close()
    else:
        images = list()
        for image in input_images:
            if image is not None:
                images.append(image)
        fout = open(PATH + prefix + '_list' + '.txt', 'w')
        for image in images:
            fout.write(image)
            fout.write('\n')
        fout.close()

    if method == 'identical':
        if os.path.isfile(PATH + prefix + '_sim_' + method + '.npz'):
            sim = scipy.sparse.load_npz(PATH + prefix + '_sim_' + method +
                                        '.npz')
        else:
            funs = [
                imagehash.average_hash, imagehash.phash, imagehash.dhash,
                imagehash.whash
            ]

            im_objs = list()
            for image in images:
                im_objs.append(Image.open(PATH + image))

            print('images', len(images), 'im_objs', len(im_objs))

            vs = list()
            for i in xrange(len(im_objs)):
                obj_i = im_objs[i]
                v_i = np.array([fun(obj_i) for fun in funs])
                vs.append(v_i)

            sim = dok_matrix((len(images), len(images)), dtype=np.float32)
            for i in xrange(len(images)):

                current_t = time.time()

                v_i = vs[i]
                for j in xrange(len(images)):
                    v_j = vs[j]
                    s = np.median(v_i - v_j)
                    if s < IDENTICAL_T:
                        sim[i, j] = (IDENTICAL_T - s) / IDENTICAL_T

                print('processing images ', i, 100 * i // len(images),
                      time.time() - current_t, 's')

            sim = sim.asformat('csr')
            scipy.sparse.save_npz(PATH + prefix + '_sim_' + method + '.npz',
                                  sim)

    if method in [
            'vgg16', 'vgg19', 'xception', 'inception_resnet_v2', 'vggface'
    ]:
        if method == 'vgg16':
            from keras.applications.vgg16 import VGG16
            from keras.preprocessing import image as keras_image
            from keras.applications.vgg16 import preprocess_input
            model = VGG16(weights='imagenet', include_top=False)
        if method == 'vgg19':
            from keras.applications.vgg19 import VGG19
            from keras.preprocessing import image as keras_image
            from keras.applications.vgg19 import preprocess_input
            model = VGG19(weights='imagenet', include_top=False)
        if method == 'xception':
            from keras.applications.xception import Xception
            from keras.preprocessing import image as keras_image
            from keras.applications.xception import preprocess_input
            model = Xception(weights='imagenet', include_top=False)
        if method == 'inception_resnet_v2':
            from keras.applications.inception_resnet_v2 import InceptionResNetV2
            from keras.preprocessing import image as keras_image
            from keras.applications.inception_resnet_v2 import preprocess_input
            model = InceptionResNetV2(weights='imagenet', include_top=False)
        if method == 'vggface':
            print('vggface')
            from keras_vggface.vggface import VGGFace
            from keras.preprocessing import image as keras_image
            from keras_vggface.utils import preprocess_input
            model = VGGFace(include_top=False)

        def get_feature(img_path):
            img = keras_image.load_img(img_path, target_size=(224, 224))
            x = keras_image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            feature = model.predict(x)
            return feature

        if os.path.isfile(PATH + prefix + '_embeddings_' + method + '.npy'):
            embeddings = np.load(PATH + prefix + '_embeddings_' + method +
                                 '.npy')
        else:
            print('get image features')  #debug
            embeddings = list()
            for image in images:
                embeddings.append(get_feature(PATH + image).flatten())
                print('process', image)
            embeddings = np.array(embeddings, dtype=np.float32)

            np.save(PATH + prefix + '_embeddings_' + method + '.npy',
                    embeddings)

        if os.path.isfile(PATH + prefix + '_sim_' + method + '.npz'):
            sim = scipy.sparse.load_npz(PATH + prefix + '_sim_' + method +
                                        '.npz')
        else:
            lsh_instance = lsh.LSH(8, 5)
            indices = lsh_instance.load(embeddings)
            sim = dok_matrix((len(images), len(images)), dtype=np.float32)
            for i in range(len(images)):

                v_i = embeddings[i]
                for j in lsh_instance.query(indices[i]):
                    v_j = embeddings[j]
                    sim[i, j] = similarity(v_i, v_j)

                sys.stdout.write("\r%d%%" % (100 * i // len(images)))
                sys.stdout.flush()

            sim = sim.asformat('csr')
            scipy.sparse.save_npz(PATH + prefix + '_sim_' + method + '.npz',
                                  sim)

    image2eid = dict(zip(images, range(len(images))))
    image2eid[None] = len(images)

    return image2eid, sim
示例#15
0
                                                class_mode='categorical')
print(trainGen320.class_indices)
print(valicGen320.class_indices)

# In[3]:

#set model layers
import keras
from keras.models import Model, Sequential  #导入模型
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout, GlobalAveragePooling2D  #导入连接层
#from spp.SpatialPyramidPooling import SpatialPyramidPooling
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions

modelA = InceptionResNetV2(include_top=False,
                           weights='imagenet',
                           input_shape=(None, None, 3))
x = modelA.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5, seed=seed)(x)
predictions = Dense(2, activation='softmax')(x)
modelA = Model(inputs=modelA.input, outputs=predictions)

# In[4]:

#train model
from keras.optimizers import Adam, SGD
from keras.layers import *
from keras.models import *
from keras.optimizers import *
                                                    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 = InceptionResNetV2(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(128, 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]:
示例#17
0
from util import map_scores
from pathlib import Path
import numpy as np
import tensorflow as tf
import time

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.keras.backend.set_session(tf.Session(config=config))

start_time = time.time()

dim_size = 224
input_tensor = Input(shape=(dim_size, dim_size, 3))

model = InceptionResNetV2(input_tensor=input_tensor, weights=None, classes=7)
model.load_weights('weights_inceptionresnet224.h5', by_name=False)

img_path = str(
    Path(__file__).resolve().parents[1]) + '/test_imgs/keratosis1.jpg'
img = image.load_img(img_path, target_size=(dim_size, dim_size))
x = image.img_to_array(img)
x = x / 255.0
x = np.expand_dims(x, axis=0)
#x = preprocess_input(x)

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', map_scores(preds, 3)[0])
示例#18
0
       'Fresh herbs', 'Nectarines, peaches & apricots',
       'Lunch & Deli Meats', 'Milk', 'Potatoes',
       'Broccoli, cauliflowers, carrots & radish',
       'Asparagus, string beans & brussels sprouts', 'Poultry',
       'Pineapples, melons & passion fruit']

## create mapping from for prediction score
classes = train_generator.class_indices 
class_map = {value:key for key,value in classes.items()}
#print (class_map)

"""**Loading the pre-trained model**"""

# load the pre-trained model
base_model = InceptionResNetV2(weights='imagenet', 
                         include_top=False, 
                         input_shape=(HEIGHT,WIDTH,3))

#Fine-tuning the pretrained model using the dataset
def build_finetune_model(base_model, dropout,fc_layers, num_classes, fine_tune_at):
    for layer in base_model.layers[:fine_tune_at]:
        layer.trainable = False
        
    x = base_model.output
    #x = Flatten()(x)
    x = GlobalAveragePooling2D(name='max_pool')(x)
    x = GaussianNoise(0.1)(x)
    
    for fc in fc_layers:
        # New FC layer, random init
        x = Dense(fc,activation='relu')(x)
def create_model(model_type, weights=None):
    """
    Function to create Neural Network model for recognizing TV News scenes.

    :param model_type: See const_spec class Architecture.
    :param weights: Path to the model, which weights will be used.
    :return: created model, optimizer, batch size and epochs
    """

    # Model architecture
    if model_type in Architecture.VGG16.all:
        model = VGG16(include_top=False, input_shape=INPUT_SHAPE)

    elif model_type in Architecture.InceptionResNetV2.all:
        model = InceptionResNetV2(
            include_top=False,
            input_shape=INPUT_SHAPE,
            weights=('imagenet' if weights is None else None))

    elif model_type in Architecture.InceptionV3.all:
        model = InceptionV3(include_top=False, input_shape=INPUT_SHAPE)

    elif model_type in Architecture.MobileNetV2.all:
        model = MobileNetV2(include_top=False, input_shape=INPUT_SHAPE)

    else:
        raise Exception('[E] Required architecture does not exist!')

    # Name of the model
    model.name = model_type

    # Stacking top layers
    if '_LSTM' in model_type:
        lstm = Sequential()
        lstm.add(TimeDistributed(model, input_shape=INPUT_SHAPE_TD))

        if '_FS_' in model_type or '_FDS_' in model_type:
            lstm.add(TimeDistributed(Flatten()))
        elif '_MS_' in model_type or '_MDS_' in model_type:
            lstm.add(TimeDistributed(GlobalMaxPooling2D()))

        elif '_AS_' in model_type or '_ADS_' in model_type:
            lstm.add(TimeDistributed(GlobalAveragePooling2D()))
        else:
            raise Exception('[E] Top layers does not exists: %s' % model_type)

        if '_FDS_' in model_type or '_MDS_' in model_type or '_ADS_' in model_type:
            lstm.add(TimeDistributed(Dense(1792, activation='relu')))
            lstm.add(TimeDistributed(Dropout(0.2)))

        if weights is not None:
            # store weights into a new model (must be made like this)
            model_weights = keras.models.load_model(weights)
            model_weights.layers.pop()

            model.set_weights(model_weights.get_weights())
            for layer in model.layers:
                layer.trainable = False

        if '_LSTM16_' in model_type:
            lstm.add(LSTM(16))
        elif '_LSTM64_' in model_type:
            lstm.add(LSTM(64))
        elif '_LSTM128_' in model_type:
            lstm.add(LSTM(128))
        else:
            lstm.add(LSTM(32))

        lstm.add(Dense(NUM_CLASSES, activation='softmax'))

        model = lstm
    elif '_FS_' in model_type:
        x = model.layers[-1].output
        x = Flatten()(x)

        predictions = Dense(NUM_CLASSES, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)

    elif '_FDS_' in model_type:
        x = model.layers[-1].output
        x = Flatten()(x)
        x = Dense(1792, activation='relu')(x)
        x = Dropout(0.2)(x)

        predictions = Dense(NUM_CLASSES, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)

    elif '_AS_' in model_type:
        x = model.layers[-1].output
        x = GlobalAveragePooling2D()(x)

        predictions = Dense(NUM_CLASSES, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)

    elif '_ADS_' in model_type:
        x = model.layers[-1].output
        x = GlobalAveragePooling2D()(x)
        x = Dense(1792, activation='relu')(x)
        x = Dropout(0.2)(x)

        predictions = Dense(NUM_CLASSES, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)

    elif '_MS_' in model_type:
        x = model.layers[-1].output
        x = GlobalMaxPooling2D()(x)

        predictions = Dense(NUM_CLASSES, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)

    elif '_MDS_' in model_type:
        x = model.layers[-1].output
        x = GlobalMaxPooling2D()(x)
        x = Dense(1792, activation='relu')(x)
        x = Dropout(0.2)(x)

        predictions = Dense(NUM_CLASSES, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)

    else:
        raise Exception('[E] Top layers does not exists: %s' % model_type)

    # Optimizer
    if 'LSTM' in model_type:
        if 'Adam' in model_type:
            optimizer = keras.optimizers.Adam(lr=0.0001,
                                              beta_1=0.9,
                                              beta_2=0.999,
                                              amsgrad=False)
        elif 'AMSGrad' in model_type:
            optimizer = keras.optimizers.Adam(lr=0.0001,
                                              beta_1=0.9,
                                              beta_2=0.999,
                                              amsgrad=True)
        elif 'SGD' in model_type:
            optimizer = keras.optimizers.SGD(lr=0.001,
                                             momentum=0.9,
                                             nesterov=True)
        elif 'RMSprop' in model_type:
            optimizer = keras.optimizers.RMSprop(lr=0.00001, rho=0.9)
        else:
            raise Exception('[E] Optimizer not found!')
    # not time-distributed model
    elif 'Adam' in model_type:
        optimizer = keras.optimizers.Adam(lr=0.000001,
                                          beta_1=0.9,
                                          beta_2=0.999,
                                          amsgrad=False)
    elif 'AMSGrad' in model_type:
        optimizer = keras.optimizers.Adam(lr=0.000001,
                                          beta_1=0.9,
                                          beta_2=0.999,
                                          amsgrad=True)
    elif 'SGD' in model_type:
        optimizer = keras.optimizers.SGD(lr=0.001, momentum=0.9, nesterov=True)
    elif 'RMSprop' in model_type:
        optimizer = keras.optimizers.RMSprop(lr=0.00025, rho=0.9)
    else:
        raise Exception('[E] Optimizer not found!')

    return model, optimizer, BatchSize[model_type], EPOCHS
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input

model_dict['model name'] = "VGG16"
model_dict['model'] = VGG16(include_top=False, weights='imagenet')
model_dict['preprocess input'] = preprocess_input
# ExtractFeatures(model_dict, some_train_img_list, (224, 224, -1), "train")
# ExtractFeatures(model_dict, some_test_image_list, (224, 224, -1), "test")
model_dict.clear()

from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.inception_resnet_v2 import preprocess_input

model_dict['model name'] = "InceptionResNetV2"
model_dict['model'] = InceptionResNetV2(include_top=False, weights='imagenet')
model_dict['preprocess input'] = preprocess_input
# ExtractFeatures(model_dict, some_train_img_list, (299, 299, -1), "train")
# ExtractFeatures(model_dict, some_test_image_list, (299, 299, -1), "test")
model_dict.clear()

from keras.applications.nasnet import NASNetLarge
from keras.applications.nasnet import preprocess_input

model_dict['model name'] = "NASNetLarge"
model_dict['model'] = NASNetLarge(input_shape=(331, 331, 3),
                                  include_top=False,
                                  weights='imagenet')
model_dict['preprocess input'] = preprocess_input
ExtractFeatures(model_dict, some_train_img_list, (331, 331, -1), 'train')
ExtractFeatures(model_dict, some_test_image_list, (331, 331, -1), 'test')
示例#21
0
def input_model(x_train, y_train, x_val, y_val, params):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.Session(config=config)
    K.set_session(session)
    # input generators
    #IMAGE_SIZE = (params['image_size'], params['image_size'])
    IMAGE_SIZE = (240, 240)
    train_datagen = ImageDataGenerator(rotation_range=5,
                                       width_shift_range=0.1,
                                       height_shift_range=0.1,
                                       brightness_range=(0.85, 1.15),
                                       shear_range=0.0,
                                       zoom_range=0.2,
                                       channel_shift_range=0.2,
                                       fill_mode='reflect',
                                       horizontal_flip=True,
                                       vertical_flip=False,
                                       preprocessing_function=preprocess)
    valid_datagen = ImageDataGenerator(preprocessing_function=preprocess)

    train = train_datagen.flow_from_directory(TRAIN_DIR,
                                              target_size=IMAGE_SIZE,
                                              color_mode='rgb',
                                              batch_size=BATCH_SIZE,
                                              interpolation='bicubic')
    valid = valid_datagen.flow_from_directory(VAL_DIR,
                                              target_size=IMAGE_SIZE,
                                              color_mode='rgb',
                                              batch_size=BATCH_SIZE,
                                              interpolation='bicubic')

    # model
    input_tensor = keras.layers.Input(shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3))
    base_model = InceptionResNetV2(include_top=False,
                                   weights='imagenet',
                                   input_tensor=input_tensor,
                                   input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1],
                                                3),
                                   pooling=None,
                                   classes=N_CLASSES)

    x = base_model.output
    x = Flatten()(x)
    x = Dense(64,
              kernel_initializer='he_normal',
              kernel_regularizer=keras.regularizers.l2(1e-6))(x)
    x = BatchNormalization()(x)
    x = PReLU()(x)

    x = Dropout(0.2)(x)
    x = Dense(64,
              kernel_initializer='he_normal',
              kernel_regularizer=keras.regularizers.l2(1e-6))(x)
    x = BatchNormalization()(x)
    x = PReLU()(x)

    if params['deep_layers'] >= 3:
        x = Dropout(0.2)(x)
        x = Dense(64,
                  kernel_initializer='he_normal',
                  kernel_regularizer=keras.regularizers.l2(1e-6))(x)
        x = BatchNormalization()(x)
        x = PReLU()(x)

    if params['deep_layers'] == 4:
        x = Dropout(0.2)(x)
        x = Dense(64,
                  kernel_initializer='he_normal',
                  kernel_regularizer=keras.regularizers.l2(1e-6))(x)
        x = BatchNormalization()(x)
        x = PReLU()(x)

    predictions = Dense(N_CLASSES, activation='softmax')(x)
    model = keras.models.Model(inputs=base_model.input, outputs=predictions)
    for layer in model.layers[:params['freeze_layers']]:
        layer.trainable = False

    LR_BASE = 0.1
    decay = LR_BASE / (EPOCHS)
    sgd = keras.optimizers.SGD(lr=LR_BASE,
                               decay=decay,
                               momentum=0.9,
                               nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    # callbacks
    checkpoint_path = os.path.join(CHECKPOINT_PATH,
                                   'model_{}_checkpoints'.format(MODEL_NO))
    if not os.path.isdir(checkpoint_path):
        os.makedirs(checkpoint_path)
    ckpt = keras.callbacks.ModelCheckpoint(os.path.join(
        checkpoint_path, 'model.{epoch:02d}-{val_acc:.2f}.h5'),
                                           monitor='val_acc',
                                           verbose=1,
                                           save_best_only=True)
    reduce_lr = callbacks.ReduceLROnPlateau(monitor='val_acc',
                                            factor=0.2,
                                            patience=5,
                                            verbose=1,
                                            mode='auto',
                                            min_delta=0.001,
                                            cooldown=0,
                                            min_lr=0)
    early_stopping = callbacks.EarlyStopping(monitor='val_acc',
                                             min_delta=0.001,
                                             patience=10)
    log_dir = "logs/model_{}_{}".format(
        MODEL_NO,
        datetime.utcnow().strftime("%d%m%Y_%H%M%S"))
    if not os.path.isdir(log_dir):
        os.makedirs(log_dir)
    tensorboard = callbacks.TensorBoard(log_dir)

    out = model.fit_generator(
        train,
        steps_per_epoch=train.n / train.batch_size,
        epochs=EPOCHS,
        validation_data=valid,
        validation_steps=valid.n / valid.batch_size,
        callbacks=[ckpt, reduce_lr, early_stopping, tensorboard])

    return out, model
示例#22
0
def gen_base_model():
    # sub_base_model = ResNet50(weights='imagenet', include_top=False)
    sub_base_model = InceptionResNetV2(weights='imagenet', include_top=False)
    x = sub_base_model.output
    x = GlobalAveragePooling2D()(x)
    return Model(inputs=sub_base_model.input, outputs=x)
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.preprocessing import image
from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions
from PIL import Image
import numpy as np
import os
import time

model = InceptionResNetV2(weights='imagenet')

images = os.listdir('resources')
times = []

for img_path in images:
    img = image.load_img('resources/' + img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    start_time = time.time()
    preds = model.predict(x)
    times.append(time.time() - start_time)
    # decode the results into a list of tuples (class, description, probability)
    # (one such list for each sample in the batch)
    print('Predicted for ' + img_path + ": ",
          decode_predictions(preds, top=3)[0])

print("Average prediction time: %s seconds" % np.mean(times))
示例#24
0
    shear_range=0.2, 
    zoom_range=0.2, 
    horizontal_flip=True, 
    rescale=1/255,
)

val_datagen = ImageDataGenerator(
    rescale=1/255
)

train_generator = train_datagen.flow_from_dataframe(train_df, x_col='image',y_col='clas',classes=classes)
val_generator = val_datagen.flow_from_dataframe(val_df,x_col='image',y_col='clas',classes=classes)

from keras.applications.inception_resnet_v2 import InceptionResNetV2

inceptionresnet = InceptionResNetV2(include_top=False, input_shape=(256,256,3),classes=3)

inceptionresnet.trainable = False

last_layer = inceptionresnet.layers[-1].output

x = GlobalAveragePooling2D()(last_layer)
x = Dense(3,activation='softmax')(x)

model = Model(inceptionresnet.inputs,x)

model.summary()

model.compile(loss='categorical_crossentropy',optimizer=keras.optimizers.RMSprop(learning_rate=0.0001),metrics=['acc'])

history = model.fit(train_generator,epochs=3,validation_data=val_generator)
示例#25
0
        self.__dict__.update(pmodel.__dict__)
        self._smodel = ser_model

    def __getattribute__(self, attrname):
        """
        Override load and save methods to be used from the serial-model.
        The serial-model holds references to the weights in the multi-gpu model.
        """
        if 'load' in attrname or 'save' in attrname:
            return getattr(self._smodel, attrname)
        else:
            return super(ModelMGPU, self).__getattribute__(attrname)


# MODEL DEFINITION
base_model = InceptionResNetV2(include_top=False, weights=None, input_shape=(IMG_SIZE, IMG_SIZE, 3))

model = models.Sequential()

model.add(base_model)
model.add(layers.Flatten())

model.add(layers.Dense(128, activation='softmax', kernel_initializer='random_uniform', bias_initializer='zeros'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid', kernel_initializer='random_uniform', bias_initializer='zeros'))

model.summary()

# TRAINING SETUP
if GPU > 0:
    run_model = ModelMGPU(model, GPU)
print(X_datas.shape)
print(x_train.shape)
print(x_test.shape)
print(x_val.shape)
#print(np.asarray(range(len(uc))))
#print(y_val[0,:])

# ## Adapting the InceptionResNetV2 Architecture to Regression Problems

# In[8]:

from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.models import Model
model = InceptionResNetV2(weights='imagenet',
                          include_top=True,
                          input_shape=(299, 299, 3))
x = model.get_layer(index=len(model.layers) - 2).output

print(x)
x = Dense(1)(x)
model = Model(inputs=model.input, outputs=x)
model.summary()

# **Using RMSprop optimizer, mean absolute error with metrics, and mean square erro with loss**

# In[ ]:

opt = RMSprop(lr=0.0001)
model.compile(loss='mean_squared_error', optimizer=opt, metrics=['mae'])
from keras.preprocessing import image
from keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions, InceptionResNetV2
import numpy as np

resnet = InceptionResNetV2(include_top=True,
                           weights='imagenet',
                           input_tensor=None,
                           input_shape=None,
                           pooling=None,
                           classes=1000)

################################### TEST IMAGES ###################################
test_image1 = "Dataset/Food-101/images/cup_cakes/15425.jpg"
test_image2 = "Dataset/food-101/images/baby_back_ribs/2432.jpg"
test_image3 = "Dataset/Food-101/images/pizza/32004.jpg"
test_image4 = "Dataset/UECFood100/1/1.jpg"

img_path = test_image2

img = image.load_img(img_path, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
#x = preprocess_input(x)

preds = resnet.predict(x)

print(decode_predictions(preds))
def get_test_neural_net(type):
    model = None
    if type == 'mobilenet_small':
        from keras.applications.mobilenet import MobileNet
        model = MobileNet((128, 128, 3),
                          depth_multiplier=1,
                          alpha=0.25,
                          include_top=True,
                          weights='imagenet')
    elif type == 'mobilenet':
        from keras.applications.mobilenet import MobileNet
        model = MobileNet((224, 224, 3),
                          depth_multiplier=1,
                          alpha=1.0,
                          include_top=True,
                          weights='imagenet')
    elif type == 'mobilenet_v2':
        from keras.applications.mobilenetv2 import MobileNetV2
        model = MobileNetV2((224, 224, 3),
                            depth_multiplier=1,
                            alpha=1.4,
                            include_top=True,
                            weights='imagenet')
    elif type == 'resnet50':
        from keras.applications.resnet50 import ResNet50
        model = ResNet50(input_shape=(224, 224, 3),
                         include_top=True,
                         weights='imagenet')
    elif type == 'inception_v3':
        from keras.applications.inception_v3 import InceptionV3
        model = InceptionV3(input_shape=(299, 299, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'inception_resnet_v2':
        from keras.applications.inception_resnet_v2 import InceptionResNetV2
        model = InceptionResNetV2(input_shape=(299, 299, 3),
                                  include_top=True,
                                  weights='imagenet')
    elif type == 'xception':
        from keras.applications.xception import Xception
        model = Xception(input_shape=(299, 299, 3),
                         include_top=True,
                         weights='imagenet')
    elif type == 'densenet121':
        from keras.applications.densenet import DenseNet121
        model = DenseNet121(input_shape=(224, 224, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'densenet169':
        from keras.applications.densenet import DenseNet169
        model = DenseNet169(input_shape=(224, 224, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'densenet201':
        from keras.applications.densenet import DenseNet201
        model = DenseNet201(input_shape=(224, 224, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'nasnetmobile':
        from keras.applications.nasnet import NASNetMobile
        model = NASNetMobile(input_shape=(224, 224, 3),
                             include_top=True,
                             weights='imagenet')
    elif type == 'nasnetlarge':
        from keras.applications.nasnet import NASNetLarge
        model = NASNetLarge(input_shape=(331, 331, 3),
                            include_top=True,
                            weights='imagenet')
    elif type == 'vgg16':
        from keras.applications.vgg16 import VGG16
        model = VGG16(input_shape=(224, 224, 3),
                      include_top=False,
                      pooling='avg',
                      weights='imagenet')
    elif type == 'vgg19':
        from keras.applications.vgg19 import VGG19
        model = VGG19(input_shape=(224, 224, 3),
                      include_top=False,
                      pooling='avg',
                      weights='imagenet')
    return model
示例#29
0
	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 == "resnet50":
	base_model = ResNet50(weights=weights)
	model = Model(input=base_model.input, output=base_model.get_layer('flatten').output)
	image_size = (224, 224)
elif model_name == "inceptionv3":
	base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3)))
	model = Model(input=base_model.input, output=base_model.get_layer('mixed10').output)
	image_size = (299, 299)
elif model_name == "inceptionresnetv2":
	base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3)))
	model = Model(input=base_model.input, output=base_model.get_layer('custom').output)
	image_size = (299, 299)
elif model_name == "mobilenet":
	base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224,224,3)), input_shape=(224,224,3))
	model = Model(input=base_model.input, output=base_model.get_layer('custom').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

# get all the train labels
train_labels = os.listdir(train_path)
示例#30
0
import tensorflow as tf
from keras import backend as K

from keras.applications.inception_resnet_v2 import InceptionResNetV2
from utils.data_loader import train_generator, val_generator

sess = tf.Session()
K.set_session(sess)

image_size = 100

def _float32_feature_list(floats):
    return tf.train.Feature(float_list=tf.train.FloatList(value=floats))

model = InceptionResNetV2(input_shape=(image_size, image_size, 3), include_top=False, pooling='avg')
model.summary()

# ''' TRAIN SET '''
nb_samples = 250000 * 2
batchsize = 200

with sess.as_default():
    generator = train_generator(batchsize, shuffle=False)
    writer = tf.python_io.TFRecordWriter('weights/inception_resnet_train.tfrecord')

count = 0
for _ in range(nb_samples // batchsize):
    x_batch, y_batch = next(generator)

    with sess.as_default():
        x_batch = model.predict(x_batch, batchsize, verbose=1)