Пример #1
0
def _inceptionv4(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
    
    print('Architecture: Inception-v4...')

    model = inception_v4.create_model(include_top=True, input_shape=input_shape, weights=None, num_classes=num_classes)

    return model
 def __init__(self, data=None, abstract_vector=False):
     '''
     Initialize variables.
     '''
     self.data = data
     self.cnn = inception_v4.create_model(weights_path="pretrained.h5",
                                          abstract=abstract_vector)
Пример #3
0
def save_bottlebeck_features():
    datagen = ImageDataGenerator(rescale=1. / 255)

    # build the InceptionV4 network
    model = inception_v4.create_model(include_top=False, weights='imagenet')

    generator = datagen.flow_from_directory(train_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=batch_size,
                                            class_mode=None,
                                            shuffle=False)
    bottleneck_features_train = model.predict_generator(
        generator, nb_train_samples // batch_size)
    np.save(open('bottleneck_features_train.npy', 'w'),
            bottleneck_features_train)

    generator = datagen.flow_from_directory(validation_data_dir,
                                            target_size=(img_width,
                                                         img_height),
                                            batch_size=batch_size,
                                            class_mode=None,
                                            shuffle=False)
    bottleneck_features_validation = model.predict_generator(
        generator, nb_validation_samples // batch_size)
    np.save(open('bottleneck_features_validation.npy', 'w'),
            bottleneck_features_validation)
Пример #4
0
def get_keras_objects():
    # Create model and load pre-trained weights
    model = inception_v4.create_model(weights='imagenet', include_top=True)

    sess = K.get_session()
    graph = sess.graph

    image = graph.get_tensor_by_name("input_1:0")
    embedding = graph.get_tensor_by_name("flatten_1/Reshape:0")

    return sess, graph, image, embedding
Пример #5
0
def buildmodel():
    model = inception_v4.create_model(num_classes=nb_classes, include_top=True)
    print("Model created")

    #    model.summary()
    optimizer = Adam(
        lr=0.0001)  # Using Adam instead of SGD to speed up training
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=["accuracy"])
    print("Finished compiled.")
    return model
Пример #6
0
def Inception_v4(classes_number):
    from inception_v4 import create_model
    from keras.layers.core import Dense, Dropout, Flatten
    from keras.models import Model

    base_model = create_model(weights=None)
    x = base_model.layers[-2].output
    del base_model.layers[-1:]
    x = Dense(classes_number, activation='sigmoid', name='predictions')(x)
    model = Model(base_model.input, x)
    # print(model.summary())
    return model
Пример #7
0
def predict(task):
    if (task == 'design'):
        task_list = task_list_design
        model1_path = MODEL_DESIGN_INCEPTIONV4

    else:
        task_list = task_list_length
        model1_path = MODEL_LENGTH_INCEPTIONV4

    label_names = list(task_list.keys())

    # load model 1
    base_model = inception_v4.create_model(weights='imagenet',
                                           include_top=False,
                                           width=width)
    input_tensor = Input((width, width, 3))
    x = input_tensor
    x = Lambda(preprocess_input, name='preprocessing')(x)
    x = base_model(x)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = [
        Dense(count, activation='softmax', name=name)(x)
        for name, count in task_list.items()
    ]

    model1 = Model(input_tensor, x)
    model1.load_weights(model1_path, by_name=True)

    y_pred11 = process('default', model1, width, fnames_test, n_test)
    y_pred12 = process('flip', model1, width, fnames_test, n_test)

    del model1, base_model, x, input_tensor

    # ensemble two models
    for i in range(n_test):
        problem_name = df_test.label_name[i].replace('_labels', '')
        problem_index = label_names.index(problem_name)
        probs11 = y_pred11[problem_index][i]
        probs12 = y_pred12[problem_index][i]
        probs1 = probs11 + probs12
        probs = probs1
        df_test.label[i] = ';'.join(np.char.mod('%.8f', probs))

    # write csv files
    fname_csv = 'results/test_%s.csv' % (task)
    df_test.to_csv(fname_csv, index=None, header=None)
Пример #8
0
def get_keras_objects():
    # Create model and load pre-trained weights
    scope = "inception_v4"
    with tf.name_scope(scope):
        model = inception_v4.create_model(weights='imagenet', include_top=True)

        sess = K.get_session()
        graph = sess.graph

        image = graph.get_tensor_by_name(scope + "/input_1:0")
        dropout = graph.get_tensor_by_name(scope + "/dropout_1/cond/Merge:0")
        keras_training = graph.get_tensor_by_name(
            scope + "/batch_normalization_1/keras_learning_phase:0")
        embedding = tf.contrib.layers.flatten(dropout)

    # tf.summary.FileWriter(logdir = "logs", graph = graph).flush()

    return sess, graph, image, embedding, keras_training
Пример #9
0
    def image_embedding(self):
        if self._cnn_model == 'inception_v3':
            base_model = InceptionV3(weights='imagenet',
                                     include_top=False,
                                     pooling='avg')
            for layer in base_model.layers:
                layer.trainable = self._is_trainable

            base_model_output = base_model.output

        elif self._cnn_model == 'vgg16':
            base_model = VGG16(weights='imagenet')
            for layer in base_model.layers:
                layer.trainable = self._is_trainable

            transfer_layer = base_model.get_layer('fc2')
            base_model_output = transfer_layer.output

        elif self._cnn_model == 'inception_v4':
            base_model = inception_v4.create_model(weights='imagenet',
                                                   include_top=False)
            for layer in base_model.layers:
                layer.trainable = self._is_trainable

            base_model_output = GlobalAveragePooling2D()(base_model.output)

        else:
            raise ValueError('Image CNN is not available')

        image_output = RepeatVector(self._max_seq_length)(base_model_output)
        image_output = BatchNormalization()(image_output)
        image_input = base_model.input
        image_embedding = TimeDistributed(
            Dense(units=self._embedding_size,
                  kernel_regularizer=self._regularizer,
                  kernel_initializer=self._initializer,
                  name='image_embedding'))(image_output)
        image_dropout = Dropout(self._drop_rate,
                                name='image_dropout')(image_embedding)

        self._image_input = image_input
        self._image_embedding = image_dropout
        self._transfer_output = base_model_output
Пример #10
0
def pants_recognize(pic_path):
    width = 480  #图片的宽度,这个模型训练的时候就是取图片大小为480*480
    task_list = {"pants_category": 7}  #设置pants_category为字典{}
    base_model = inception_v4.create_model(
        weights='imagenet', width=width, include_top=False
    )  #调用inception_v4.py的create_model,并且不要最上面的全连接层,具体的在inceV4.py函数里面有解释
    input_tensor = Input((width, width, 3))  #input为keras.layers的input函数
    x = input_tensor
    x = Lambda(preprocess_input, name='preprocessing')(x)
    #Lambda函数本身的意思是对上一层函数的输出施加任何theano/tensorflow表达式,lambda函数就是加了一个网络层给他起名字叫preprocessing
    # 然后调用keras.applications.inception_resnet_v2 的 preprocess_input对图片进行预处理,详情可以参考inceptionV4的def preprocess_input(x):
    #
    #且引用keras.applications.inception_resnet_v2的图像预处理对图片进行预处理。
    x = base_model(x)  #传到base_model函数中,并且返回一个model
    x = GlobalAveragePooling2D()(x)  #为刚才得出的model()空域信号施加一个全局平均值池化
    x = Dropout(0.5)(x)  #d增加ropout有0.5的概率会“失活”
    x = [
        Dense(count, activation='softmax', name="recognize")(x)
        for name, count in task_list.items()
    ]  #怎么与前面的联系起来?
    #再添加全连接层,最后的这个全连接层接的激活函数是softmax函数
    model = Model(input_tensor, x)  #
    model.load_weights('pants_inceptionv4_add1000_0.9258.h5')

    model.summary()
    #model.save('test1.h5')

    categories = ['紧身裤', '锥形裤', '直筒裤', '喇叭裤', '阔腿裤', '灯笼裤', '不可见']
    img_path = pic_path
    X_test = np.zeros(
        (1, 480, 480, 3),
        dtype=np.uint8)  #设置X_test为1*480*480*3的4维张量,之所以第一个数字为1,是因为我们现在只有一张图片
    img = cv2.resize(cv2.imread(img_path),
                     (480, 480))  #通过cv2这个函数将输入进来的图片进行调整大小到480*480
    X_test[
        0] = img[:, :, ::
                 -1]  #第一个:取遍图像的所有行数。第二个:是取遍所有的列数。最后的::是取遍所有的通道数,-1是反向取值,则RGB变为BGR
    prediction = model.predict(X_test)  #用model进行预测
    print(prediction)  #输出7个数字,用softmax函数激活,和为1
    for i in range(7):
        if prediction[0][i] > 0.7:
            print('此图片属于' + categories[i])
            return categories[i]
def model_loader(model_name,
                 num_classes=200,
                 weights=None,
                 include_top=True,
                 phase=4,
                 freeze_layers=False,
                 img_size=299):
    # wrapper for loading models, currently only for the inceptionV4 and alexnetmodel but more to be created in the future.
    if model_name == 'inception_v4':
        return inception_v4.create_model(num_classes=num_classes,
                                         weights=weights,
                                         include_top=True,
                                         phase=phase,
                                         freeze_layers=freeze_layers)
    elif model_name == 'alexnet':
        return alexnet.create_model(num_classes=num_classes,
                                    img_size=img_size,
                                    weights=weights,
                                    phase=phase,
                                    freeze_layers=freeze_layers)
Пример #12
0
def model(input_shape, output_shape):

    inception = create_model(include_top=False,
                             weights='imagenet',
                             input_shape=input_shape)

    inception.trainable = False

    model = Sequential()
    model.add(inception)
    model.add(Flatten())
    model.add(Dense(2048))
    model.add(Activation('relu'))
    model.add(Dropout(0.33))
    model.add(Dense(1024))
    model.add(Activation('relu'))
    model.add(Dropout(0.33))
    model.add(Dense(output_shape))
    model.add(Activation('softmax'))

    return model
Пример #13
0
based_model_last_block_layer_number = 400
img_width, img_height = 229, 229
batch_size = 32
nb_epoch = 50
transformation_ratio = .5
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
top_model_weights_path = './models/top_model_weights_inception_v4.h5'
final_model_weights_path = './models/model_weights_inception_v4.h5'
model_path = './models/model_inception_v4.json'
nb_train_samples = 20000
nb_validation_samples = 5000

base_model = inception_v4.create_model(input_shape=(img_width, img_height, 3),
                                       weights='imagenet',
                                       include_top=False)

# # ... Load pre-trained VGG16 model
#
# model.layers.pop() # Get rid of the classification layer
# model.layers.pop() # Get rid of the dropout layer
# model.outputs = [model.layers[-1].output]
# model.layers[-1].outbound_nodes = []
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(base_model.input, predictions)
print(model.summary())

for layer in base_model.layers:
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
set_session(tf.Session(config=config))

import keras2caffe

import sys
sys.path.append('/home/data/keras-models/keras-inceptionV4')

import inception_v4
#import evalute_image

#converting

keras_model = inception_v4.create_model(weights='imagenet', include_top=True, dropout_prob=0.8)

keras2caffe.convert(keras_model, 'InceptionV4.prototxt', 'InceptionV4.caffemodel')

#testing the model

caffe.set_mode_gpu()
net  = caffe.Net('InceptionV4.prototxt', 'InceptionV4.caffemodel', caffe.TEST)

img = cv2.imread('bear.jpg')
#img = evaluate_image.central_crop(im, 0.875)

img = cv2.resize(img, (299, 299))
img = img[...,::-1]  #RGB 2 BGR

data = np.array(img, dtype=np.float32)
Пример #15
0
def main(args):
    # Create model and load pre-trained weights
    #model = inception_v4.create_model(weights='imagenet', include_top=True)

    # inception_93.hd5
    #train_path = '/home/s/sws007/dataset/cats1/train'
    #val_path = '/home/s/sws007/dataset/test'

    config = tf.ConfigProto()
    #config.gpu_options.per_process_gpu_memory_fraction = 0.3
    config.gpu_options.allow_growth = True
    set_session(tf.Session(config=config))

    # inception_tmp.hd5
    #train_path = '/home/s/sws007/dataset/cats'
    #val_path = '/home/s/sws007/dataset/test'

    #val_path = '/home/s/sws007/dataset/cats1/val'
    num_classes = len(os.listdir(args.train_path))

    # limit gpu resources
    #config = tf.ConfigProto()
    #config.gpu_options.per_process_gpu_memory_fraction = 0.3
    #set_session(tf.Session(config=config))

    print('number of classes:', num_classes)

    finetune = True
    if not finetune:
        model = inception_v4.create_model(num_classes=4, include_top=True)
    else:
        #prev_model = inception_v4.create_model(weights='imagenet', num_classes=4, include_top=False)
        model = inception_v4.create_model(num_classes=4, finetune=False)
        #pool = AveragePooling2D((8,8), padding='valid')(prev_model)
        #dropout = Dropout(dropout_keep_prob)(pool)
        #flatten = Flatten()(dropout)
        #fc = Dense(units=num_classes, activation='softmax')(flatten)
        #model = Model(inputs=prev_model.input, outputs=fc)

        for index, layer in enumerate(model.layers):
            print(index, layer, layer.trainable)
        #print(model.layers)

    #model.load_weights('inception1.hd5')

    #imgs, labels, classes, cls2idx = load_img()

    aug = True
    if aug:
        train_datagen = ImageDataGenerator(rotation_range=10,
                                           width_shift_range=0.05,
                                           zoom_range=0.05,
                                           channel_shift_range=10,
                                           height_shift_range=0.05,
                                           shear_range=0.05,
                                           horizontal_flip=True,
                                           rescale=1. / 255)
        #train_datagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.05, zoom_range=0.05, channel_shift_range=10, height_shift_range=0.05, shear_range=0.05, horizontal_flip=True)
    else:
        train_datagen = ImageDataGenerator(rescale=1. / 255)
        #train_datagen = ImageDataGenerator()

    target_size = (299, 299)
    train_gen = train_datagen.flow_from_directory(args.train_path,
                                                  target_size=target_size,
                                                  batch_size=args.batch_size,
                                                  class_mode='categorical',
                                                  shuffle=True)
    val_gen = ImageDataGenerator(rescale=1. / 255).flow_from_directory(
        args.val_path,
        target_size=target_size,
        batch_size=args.batch_size,
        class_mode='categorical',
        shuffle=True)

    # one_hot
    #tmp_labels = np.zeros((labels.shape[0], 4))
    #tmp_labels[np.arange(labels.shape[0]), labels] = 1
    #labels = tmp_labels

    #x_train, x_test, y_train, y_test = train_test_split(imgs, labels, test_size=0.33)
    #model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adam(lr=args.learning_rate,
                                                  decay=1e-3),
                  metrics=['accuracy'])

    #model_path = "inception_4.hdf5"
    #checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
    checkpoint = ModelCheckpoint(args.model_path)
    callbacks = [checkpoint]

    step_size_train = train_gen.n // train_gen.batch_size
    step_size_valid = val_gen.n // val_gen.batch_size

    #model.fit(x_train, y_train, batch_size=128, epochs=20, validation_data=(x_test, y_test), callbacks=callbacks_list)
    #model.fit_generator(train_gen, batch_size=128, nb_epoch=1, validation_data=train_gen, verbose=2, callbacks=callbacks_list)
    # steps_per_epoch: (int) total batches of dataset in one epoch
    # epochs: number of epochs
    # verbose: 0 = silent, 1 = progress bar, 2 = one line per epoch.
    num_epochs = 1000

    #model.fit_generator(train_gen, steps_per_epoch=step_size_train, epochs=num_epochs, validation_data=val_gen, validation_steps=step_size_valid, callbacks=callbacks)
    model.fit_generator(train_gen,
                        steps_per_epoch=step_size_train,
                        epochs=num_epochs,
                        validation_data=val_gen,
                        validation_steps=step_size_valid,
                        callbacks=callbacks)
    # Load image and convert from BGR to RGB
    im = cv2.imread(img_path)
    if im is None:
        return None
    im = np.asarray(im)[:, :, ::-1]
    im = central_crop(im, 0.875)
    im = cv2.resize(im, (299, 299))
    im = inception_v4.preprocess_input(im)
    if K.image_data_format() == "channels_first":
        im = np.transpose(im, (2, 0, 1))
    return im


if __name__ == "__main__":
    # Create model and load pre-trained weights
    model = inception_v4.create_model(num_classes=train.nb_classes,
                                      include_top=True)
    model.load_weights(train.weights_file, by_name=True)

    # Load test image!
    imgs = []
    x_ids = []
    y_ids = []
    if sys.argv[1] != "":
        img_file = sys.argv[1]
        with open(img_file, 'r') as id_file:
            ids = id_file.readlines()
            for img in ids:
                x, y = img.strip().split(",")
                img = get_processed_image(
                    os.path.join(train.image_path_prefix, x[0], x[1], x[2],
                                 x + '.jpg'))
Пример #17
0
USERID = "sws007"
PASSWORD = "******"

TMP_FILE = '/tmp/%s.jpg' % (USERID)
#dict = {0:'daisy', 1:'dandelion', 2:'roses', 3: 'sunflowers', 4:'tulips'}

resp_callback = None

# ---------------- Set up model ------------------

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

model = inception_v4.create_model(num_classes=4, finetune=False)
model.load_weights('inception_98_rescale.hd5')
#images_dir = '/home/s/sws007/dataset/test_final'
cls2idx = {'MaineCoon': 0, 'Ocelot': 1, 'Singapura': 2, 'TurkishVan': 3}
idx2cls = [k for k, v in sorted(cls2idx.items(), key=lambda x: x[1])]


def load_image(image_fname):
    img = Image.open(image_fname)
    img = img.resize((299, 299))
    imgarray = np.array(img) / 255.0
    final = np.expand_dims(imgarray, axis=0)
    return final


# predict globally to load model to GPU (This is the gpu problem of MQTT, and the professor tells me that the cpu version is not influenced)
Пример #18
0
def SelectModel(ModelName='ResNet50',
                ImgRow=224,
                ImgCol=224,
                ImgChannel=3,
                Classes=2,
                GpuNum=2):

    training_model = ModelName
    """ select DL model """
    if training_model.lower() == 'resnet50':
        from keras.applications.resnet50 import ResNet50
        # 使用全ResNet50,不加層
        model = ResNet50(weights=None,
                         include_top=True,
                         input_shape=(ImgRow, ImgCol, ImgChannel),
                         classes=Classes)
        for layer in model.layers:
            layer.trainable = True

    elif training_model.lower() == 'vgg16':
        from keras.applications.vgg16 import VGG16

        model = VGG16(weights=None,
                      include_top=False,
                      input_shape=(ImgRow, ImgCol, ImgChannel),
                      classes=Classes)
        x = model.output
        x = Dropout(0.5)(x)
        x = Flatten()(x)
        x = Dense(512, activation='relu')(x)
        x = Dropout(0.5)(x)
        predictions = Dense(Classes, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)
        for layer in model.layers:
            layer.trainable = True

    elif training_model.lower() == 'vgg19':
        from keras.applications.vgg19 import VGG19

        model = VGG19(weights=None,
                      include_top=False,
                      input_shape=(ImgRow, ImgCol, ImgChannel),
                      classes=Classes)
        x = model.output
        x = Dropout(0.5)(x)
        x = Flatten()(x)
        x = Dense(512, activation='relu')(x)
        x = Dropout(0.5)(x)
        predictions = Dense(Classes, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)
        for layer in model.layers:
            layer.trainable = True

    elif training_model.lower() == 'inception_v3':
        from keras.applications.inception_v3 import InceptionV3
        model = InceptionV3(weights=None,
                            include_top=True,
                            input_shape=(ImgRow, ImgCol, ImgChannel),
                            classes=Classes)
        for layer in model.layers:
            layer.trainable = True

    elif training_model.lower() == 'inception_v4':
        import inception_v4

        model = inception_v4.create_model(dropout_prob=0.2,
                                          weights='imagenet',
                                          include_top=True)
        for layer in model.layers:
            layer.trainable = False
        x = model.output
        predictions = Dense(Classes, activation='softmax')(x)
        model = Model(inputs=model.input, outputs=predictions)
    """ enable multiGPU """
    try:
        Model = multi_gpu_model(model, gpus=GpuNum)
        print("Training using multiple GPUs..")
    except ValueError:
        Model = model
        print("Training using single GPU or CPU..")

    SingleModel = model
    return Model, SingleModel
    n = 1
    for f in imgs:
        image = cv2.imread('augmentation/'+f)
        im = get_processed_image(image)
        out = model.predict(im)
        features[n] = out
        n += 1
    shutil.rmtree('augmentation')
    os.mkdir('augmentation')
    feature = np.array(features)
    return feature


if __name__ == '__main__':
    first_learn = True #设定为第一次学习
    model = inception_v4.create_model(weights='imagenet', include_top=True)
    classes = 1  #类别计数
    class_name = [] #存储类别名字
    class_count = np.zeros(1000,dtype='int')
    #判断临时文件夹时候是否存在,augmentation文件夹用于保存数据增强时生成的文件
    dir = os.path.exists('augmentation')
    if not dir:
        os.mkdir('augmentation')
    #用于存储每一类特征向量每一位的最大最小
    features_max = np.zeros((1000,1536),dtype='float32')
    features_min = np.zeros((1000,1536), dtype='float32')
    W = np.zeros((1000,1536),dtype='float32') #存储每一类特征向量的均值
    cv2.namedWindow('image')
    while (True):
        filename = tkFileDialog.askopenfilename(initialdir='/home/deep/dataset/ourdata')#文件选择框
        img = load_img(filename)
Пример #20
0
                if not any(word in layer.name for word in bad):
                    result.append(layer)
        except:
            continue
    bn, cv, fn = result[:int((len(result) - 1) /
                             2)], result[int((len(result) - 1) /
                                             2):], result[-1]
    res_zipped = zip(cv, bn)
    out_prep = [list(elem) for elem in res_zipped]
    out = out_prep + [[fn]]
    return out


K.set_image_dim_ordering('tf')

th_model = inception_v4.create_model(
    weights="../weights/inception-v4_weights_tf_dim_ordering_tf_kernels.h5")

convert_all_kernels_in_model(th_model)
print("Converted all Kernels")

th_layers = get_layers(th_model)
th_layers = list(itertools.chain.from_iterable(th_layers))

weights = []
for th_layer in th_layers:
    weights.append(th_layer.get_weights())
    print("Saving layer: " + th_layer.name)

pickle.dump(weights, open("weights_tf_dim_ordering_th_kernels.p", "wb"))
print("Saved Pickle of Weights")
Пример #21
0
    im = np.asarray(cv2.imread(img_path))[:, :, ::-1]
    im = central_crop(im, 0.875)
    im = cv2.resize(im, (299, 299))
    im = inception_v4.preprocess_input(im)
    if K.image_data_format() == "channels_first":
        im = np.transpose(im, (2, 0, 1))
        im = im.reshape(-1, 3, 299, 299)
    else:
        im = im.reshape(-1, 299, 299, 3)
    return im


if __name__ == "__main__":

    # Create model and load pre-trained weights
    intermediate_layer_model = inception_v4.create_model(weights='vectorizer',
                                                         include_top=True)

    #read images in data/ folder:
    images = os.listdir('data/')

    features = pd.DataFrame(np.zeros((len(images), 1537)))
    #loop over images, extract features and saves it to .csv
    num_images = len(images)
    for i, image in enumerate(images):
        features.iloc[i, 0] = image.split('_')[0]
        img = get_processed_image('data/' + image)

        intermediate_output = intermediate_layer_model.predict(img)
        features.iloc[i, 1:] = np.squeeze(intermediate_output)
        print('Progress: {}'.format(i * 100 / num_images))
Пример #22
0
			layer = model.layers[i]
			if layer.trainable:
				bad = ["pooling", "flatten", "dropout", "activation", "concatenate"]
				if not any(word in layer.name for word in bad):
					result.append(layer)
		except:
			continue
	bn,cv,fn=result[:int((len(result)-1)/2)],result[int((len(result)-1)/2):],result[-1]
	res_zipped = zip(cv, bn)
	out_prep = [list(elem) for elem in res_zipped]
	out = out_prep + [[fn]]
	return out


if __name__ == "__main__":
	model = inception_v4.create_model()

	with open('weights.p', 'rb') as fp:
		weights = pickle.load(fp)

	# Get layers to set
	layers = get_layers(model)
	layers = list(itertools.chain.from_iterable(layers))

	# Set the layer weights
	setWeights(layers, weights)

	# Save model weights in h5 format
	model.save_weights("../weights/inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5")
	print("Finished saving weights in h5 format")
def train(task):
    if (task == 'design'):
        task_list = task_list_design
    else:
        task_list = task_list_length

    label_names = list(task_list.keys())
    print(n)
    y = [np.zeros((n, task_list[x])) for x in task_list.keys()]
    for i in range(n):
        label_name = df.label_name[i]
        label = df.label[i]
        y[label_names.index(label_name)][i, label.find('y')] = 1

    X = getX()
    print(X.shape)
    n_train = int(n * 0.9)
    X_train = X[:n_train]
    X_valid = X[n_train:]
    y_train = [x[:n_train] for x in y]
    y_valid = [x[n_train:] for x in y]
    gen_train = Generator(X_train, y_train, batch_size=40, aug=True)

    base_model = inception_v4.create_model(weights='imagenet',
                                           width=width,
                                           include_top=False)
    input_tensor = Input((width, width, 3))
    x = input_tensor
    x = Lambda(preprocess_input, name='preprocessing')(x)
    x = base_model(x)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = [
        Dense(count, activation='softmax', name=name)(x)
        for name, count in task_list.items()
    ]

    model = Model(input_tensor, x)
    # model.load_weights('models/base.h5',by_name=True)
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['acc'])
    model2 = multi_gpu_model(model, 2)

    model2.compile(optimizer=Adam(0.0001),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=3,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.000025),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=2,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.00000625),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=3,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.00000425),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=1,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.000001),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=1,
                         validation_data=(X_valid, y_valid))
    model.save_weights('models/%s.h5' % model_name)

    del X
    del model
    gc.collect()
Пример #24
0
    batch_size = 16

    # path
    root_path = "../planet/"
    imgs_path = root_path + "train-jpg/"
    labels_file = root_path + "train_validation_v2_bin.csv"

    # iterations config
    max_iteration = 500
    summary_iters = 50
    valid_iters = 250
    usecols = range(1, 18)


# create the base pre-trained model
base_model = create_model(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
#x = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(x)
#x = AveragePooling2D((8,8), padding='valid')(x)
#x = Dropout(0.2)(x)
#x = Flatten()(x)
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
# and a logistic layer -- 17 classes
predictions = Dense(17, activation='softmax')(x)

# this is the model we will train
print "init model"
Пример #25
0
  
  
  total_samples_test = getNumSamples(variants[num_variant][1][0:4]+'.h5')
  
  #x_test = np.zeros((total_samples_test, INPUT_FRAME_SIZE, INPUT_FRAME_SIZE, 3), dtype='float16')
  #y_test = np.zeros((total_samples_test,1), dtype='float16')
 
  y_test = np_utils.to_categorical(y_t, NUMBER_OF_CLASSES)  
  print('Test dataset loaded')
  
  print('Testing dataset size = ', x_test.shape)
  # Convert class vectors to binary class matrices
  
  print('Loading model')
  # Create Inception V4 model
  model = inception_v4.create_model(num_classes=NUMBER_OF_CLASSES, dropout_keep_prob=0.2, weights=None)
  print("Compiling model")
  rms_prop = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
  model.compile(loss='categorical_crossentropy',
                optimizer=rms_prop,
                metrics=['accuracy', 'mse'])
  print("Model loaded and compiled")
  
  # autosave best Model
  best_model_file = '{}_{}_{}_B{}_E{}_F{}.h5'.format(NET_NAME, TRAIN_SET[-11:-7], TEST_SET[-11:-7], BATCH_SIZE, EPOCHS, INPUT_FRAME_SIZE)
  best_model = ModelCheckpoint(best_model_file, monitor='val_loss', verbose=1, save_best_only=True)
  
  print("*************************************")
  print("Fitting model")
  print("*************************************")
  
Пример #26
0
from PIL import Image
import inception_v4
import numpy as np
import os

os.environ['CUDA_VISIBLE_DEVICES'] = ''


def preprocess_input(x):
    x = np.divide(x, 255.0)
    x = np.subtract(x, 1.0)
    x = np.multiply(x, 2.0)
    return x


if __name__ == "__main__":
    model = inception_v4.create_model(
        weights_path="inception_v4_pretrained.h5")

    classes = eval(open('classes.txt', 'r').read())

    img_path = 'elephant.jpg'
    im = Image.open(img_path).resize((299, 299))
    im = np.array(im)
    im = preprocess_input(im)
    im = im.reshape(-1, 299, 299, 3)

    preds = model.predict(im)
    print("Class is: " + classes[np.argmax(preds) - 1])
    print("Certainty is: " + str(preds[0][np.argmax(preds)]))
Пример #27
0
def train():
    # base_model = inception_v4.create_model(weights='imagenet', width=width, include_top=False)
    # plot_model(base_model, to_file='base_model.png')
    # input_tensor = Input((width, width, 3))
    # x = input_tensor
    # x = Lambda(preprocess_input, name='preprocessing')(x)
    # x = base_model(x)
    # x = GlobalAveragePooling2D()(x)
    # x = Dropout(0.5)(x)
    # x = [Dense(count, activation='softmax', name=name)(x) for name, count in task_list.items()]
    #
    # model2 = Model(input_tensor, x)

    # if (task == 'train'):
    #     task_list = task_list
    # else:
    #     task_list = task_list_length
    labelname = {
        'Apple': '0-5',
        'Cherry': '6-8',
        'Corn': '9-16',
        'Grape': '17-23',
        'Citrus': '24-26',
        'Peach': '27-29',
        'Pepper': '30-32',
        'Potato': '33-37',
        'Strawberry': '38-40',
        'Tomato': '41-60'
    }
    # label_names = list(task_list.keys())
    dfs = [n, m]
    for d in dfs:
        y = [np.zeros((d, task_list[x])) for x in task_list.keys()]
        for i in range(d):
            # label_name = df.label_name[i]
            if d == n:
                label = df.label[i]
            else:
                label = val_df.label[i]
            # print(i, label)
            if 0 <= label <= 23:

                # label = label - 0
                y[0][i, label] = 1

            if 24 <= label <= 40:
                label1 = label - 24
                y[1][i, label1] = 1

            if 41 <= label <= 60:
                label2 = label - 41
                y[2][i, label2] = 1
            # if 0<=label<=5:
            #
            #     # label = label - 0
            #     y[0][i, label] = 1
            # if 6<=label<=8:
            #     label1 = label - 6
            #     y[1][i, label1] = 1
            # if 9<=label<=16:
            #     label2 = label - 9
            #     y[2][i, label2] = 1
            # if 17<=label<=23:
            #     label3 = label - 17
            #     y[3][i, label3] = 1
            # if 24<=label<=26:
            #     label4 = label - 24
            #     y[4][i, label4] = 1
            # if 27<=label<=29:
            #     label5 = label - 27
            #     y[5][i, label5] = 1
            # if 30<=label<=32:
            #     label6 = label - 30
            #     y[6][i, label6] = 1
            # if 33<=label<=37:
            #     label7 = label - 33
            #     y[7][i, label7] = 1
            # if 38<=label<=40:
            #     label8 = label - 38
            #     y[8][i, label8] = 1
            # if 41<=label<=60:
            #     label9 = label - 41
            #     y[9][i, label9] = 1
        if d == n:
            y_train = y
        elif d == m:
            y_valid = y

        # y[label_names.index(label_name)][i, label.find('y')] = 1

    # print('train: ', y_train[8].tolist())
    # print('valid: ', y_valid[5].tolist())
    # print('valid: ', y_valid[0].shape)
    # for j in range(10):
    #     with open('./yvalid%d.txt'%j,"w") as f:
    #         for i in y_valid[j].tolist():
    #             f.write('%s'%i)

    X_train = getX('train')
    X_valid = getX('valid')

    tb = TensorBoard(
        log_dir='./keras_logs',  # log 目录
        histogram_freq=1,  # 按照何等频率(epoch)来计算直方图,0为不计算
        batch_size=12,  # 用多大量的数据计算直方图
        write_graph=True,  # 是否存储网络结构图
        write_grads=False,  # 是否可视化梯度直方图
        write_images=False,  # 是否可视化参数
        embeddings_freq=0,
        embeddings_layer_names=None,
        embeddings_metadata=None)
    callbacks = [tb]
    metrics = Metrics()
    print('train: ', X_train.shape)
    print('valid: ', X_valid.shape)

    gen_train = Generator(X_train, y_train, batch_size=12, aug=False)

    base_model = inception_v4.create_model(weights='imagenet',
                                           width=width,
                                           include_top=False)
    input_tensor = Input((width, width, 3))
    x = input_tensor
    x = Lambda(preprocess_input, name='preprocessing')(x)
    x = base_model(x)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = [
        Dense(count, activation='softmax', name=name)(x)
        for name, count in task_list.items()
    ]

    model2 = Model(input_tensor, x)
    print('finish...', model2.summary())
    # model.load_weights('models/base.h5',by_name=True)
    model2.compile(optimizer='adam',
                   loss='categorical_crossentropy',
                   metrics=['acc'])
    # model2 = multi_gpu_model(model, 2)
    plot_model(model2, to_file='model_3.png')

    model2.compile(optimizer=Adam(0.001),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=10,
                         validation_data=(X_valid, y_valid),
                         callbacks=callbacks)

    model2.compile(optimizer=Adam(0.00025),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=10,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.0000625),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=10,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.0000425),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=5,
                         validation_data=(X_valid, y_valid))

    model2.compile(optimizer=Adam(0.000001),
                   loss='categorical_crossentropy',
                   metrics=[acc])
    model2.fit_generator(gen_train.generator,
                         steps_per_epoch=gen_train.steps,
                         epochs=5,
                         validation_data=(X_valid, y_valid))
    model2.save_weights('models_kares/%s.h5' % model_name)

    del model
    gc.collect()
Пример #28
0
    # Load image and convert from BGR to RGB
    im = np.asarray(cv2.imread(img_path))[:, :, ::-1]
    im = central_crop(im, 0.875)
    im = cv2.resize(im, (299, 299))
    im = inception_v4.preprocess_input(im)
    if K.image_data_format() == "channels_first":
        im = np.transpose(im, (2, 0, 1))
        im = im.reshape(-1, 3, 299, 299)
    else:
        im = im.reshape(-1, 299, 299, 3)
    return im


if __name__ == "__main__":
    # Create model and load pre-trained weights
    model = inception_v4.create_model(weights='imagenet', include_top=True)

    ixs = [1, 4, 7, 381, 479]
    # blockConv = ["Conv_1","Conv_2", "Conv_3", "Conv_4", "Conv_5"]
    outputs = [model.layers[i].output for i in ixs]
    model = Model(inputs=model.inputs, outputs=outputs)

    # Open Class labels dictionary. (human readable label given ID)
    # classes = eval(open('validation_utils/class_names.txt', 'r').read())

    # Load test image!
    img_path = 'elephant.jpg'
    img = get_processed_image(img_path)

    preds = model.predict(img)
Пример #29
0
def predict(task):

    if(task=='design'):
        task_list = task_list_design
        model1_path = MODEL_DESIGN_INCEPTIONV4
        model2_path = MODEL_DESIGN_INCEPTIONRESNETV2
    else:
        task_list = task_list_length
        model1_path = MODEL_LENGTH_INCEPTIONV4
        model2_path = MODEL_LENGTH_INCEPTIONRESNETV2
    label_names = list(task_list.keys())

    base_model = inception_v4.create_model(weights='imagenet', include_top=False, width=width)
    input_tensor = Input((width, width, 3))
    x = input_tensor
    x = Lambda(preprocess_input, name='preprocessing')(x)
    x = base_model(x)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    x = [Dense(count, activation='softmax', name=name)(x) for name, count in task_list.items()]

    model1 = Model(input_tensor, x)
    model1.load_weights(model1_path, by_name=True)

    y_pred11 = process('default', model1, width, fnames_test, n_test)
    y_pred12 = process('flip', model1, width, fnames_test, n_test)

    del model1,base_model,x,input_tensor


    base_model2 = InceptionResNetV2(weights='imagenet',input_shape=(width, width, 3),include_top=False)
    input_tensor2 = Input((width, width, 3))
    x2 = input_tensor2
    x2 = Lambda(preprocess_input, name='preprocessing')(x2)
    x2 = base_model2(x2)
    x2 = GlobalAveragePooling2D()(x2)
    x2 = Dropout(0.5)(x2)
    x2 = [Dense(count, activation='softmax', name=name)(x2) for name, count in task_list.items()]

    model2 = Model(input_tensor2, x2)
    model2.load_weights(model2_path, by_name=True)

    y_pred21 = process('default', model2, width, fnames_test, n_test)
    y_pred22 = process('flip', model2, width, fnames_test, n_test)

    for i in range(n_test):
        problem_name = df_test.label_name[i].replace('_labels', '')
        problem_index = label_names.index(problem_name)
        probs11 = y_pred11[problem_index][i]
        probs12 = y_pred12[problem_index][i]
        probs21 = y_pred21[problem_index][i]
        probs22 = y_pred22[problem_index][i]

        probs1 = probs11 + probs12
        probs2 = probs21 + probs22

        probs1 = (probs1 )/2
        probs2 = (probs2) / 2

        probs = 0.5*probs1+0.5*probs2

        df_test.label[i] = ';'.join(np.char.mod('%.8f', probs))
    fname_csv = 'result/%s.csv' % (task)
    df_test.to_csv(fname_csv, index=None, header=None)

    del model2