Beispiel #1
0
def get_video_feature(label_path, video_root_path, share_video_arr,
                      share_video_label, share_video_sep_count):

    from tensorflow.python.keras.applications.resnet50 import preprocess_input

    _image, _label, _length = load_data(label_path,
                                        video_root_path,
                                        down_factor=12,
                                        th_count=4,
                                        video_root="data/TrimmedVideos/")

    print("Extract feature\n")
    sess = tf.Session(config=gpu_opt)
    K.set_session(sess)

    my_res = ResNet50(include_top=False,
                      weights="model_para/resnet.h5",
                      input_shape=[240, 320, 3])

    _feature_arr = []
    batch_size = 200
    start = 0

    for i in range(_image.shape[0] // batch_size):
        _feature_arr.append(
            my_res.predict(
                preprocess_input(_image[start:start +
                                        batch_size].astype("float"))))
        start += batch_size
    _feature_arr.append(
        my_res.predict(
            preprocess_input(_image[start:start +
                                    batch_size].astype("float"))))

    _feature_arr = np.concatenate(_feature_arr, axis=0)
    print("Finish extraction")
    sess.close()

    video_feature = []
    video_label = []
    video_sep_count = []
    start = 0

    for i, _l in enumerate(_length):
        if _l > 50:
            tmp = down_sample_50(_feature_arr[start:start + _l])
            video_feature.append(tmp)
            video_label.append(_label[start])
            video_sep_count.append(1)
        else:
            video_feature.append(_feature_arr[start:start + _l])
            video_label.append(_label[start])
            video_sep_count.append(1)
        start += _l

    print(
        "Concatenate each video to list.\nEach dimension is [ video , time , feature ]."
    )
    share_video_arr["hi"] = video_feature
    share_video_label["hi"] = video_label
    share_video_sep_count["hi"] = video_sep_count
def load_model():
    # load the pre-trained Keras model (you can use any some different
    #model or your own model here)
    global model
    model = ResNet50(weights="imagenet")
Beispiel #3
0
# grab the list of images that we'll be describing then randomly
# shuffle them to allow for easy training and testing splits via
# array sliceing during training time
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset"]))
random.shuffle(imagePaths)

# extract the class labels from the image paths then encode the
# labels
labels = [p.split(os.path.sep)[-1].split(".")[0] for p in imagePaths]
le = LabelEncoder()
labels = le.fit_transform(labels)

# load the ResNet50 network
print("[INFO] loading network...")
model = ResNet50(weights="imagenet", include_top=False)

# initialize the HDF5 dataset writer, then store the class label
# names in the dataset
dataset = HDF5DatasetWriter((len(imagePaths), 100352),
                            args["output"],
                            dataKey="features",
                            bufSize=args["buffer_size"])
dataset.storeClassLabels(le.classes_)

# initialize the progress bar
widgets = [
    "Extracting Features: ",
    progressbar.Percentage(), " ",
    progressbar.Bar(), " ",
    progressbar.ETA()
Beispiel #4
0
                                                    class_mode='binary',
                                                    target_size=(224, 224))

validation_datagen = ImageDataGenerator(
    preprocessing_function=preprocess_input)

validation_generator = validation_datagen.flow_from_directory(
    'data/validation',
    shuffle=False,
    class_mode='binary',
    target_size=(224, 224))

#------------------------------------------------------------------------------

# load pre-trained network, cut off its head and freeze its weights
conv_base = ResNet50(include_top=False, weights='imagenet')

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

x = conv_base.output
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(128, activation='relu')(x)
predictions = layers.Dense(4, activation='softmax')(x)
model = Model(conv_base.input, predictions)

optimizer = keras.optimizers.Adam()
model.compile(loss='sparse_categorical_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])
Beispiel #5
0
    clustering_model.layers[-1].outbound_nodes = []

    clustering_model.layers[0].trainable = False # this would be the base model part

    #clustering_model.outputs = [clustering_model.layers[-1].output]
    #clustering_model.layers[-1].outbound_nodes = []

    #clustering_model.layers[0].trainable = False # this would be the base model part

    #clustering_model.add(ResNet50(include_top = False, pooling='ave', weights = resnet_weigth_path))
    #clustering_model.layers[0].trainable = False

    clustering_model.add(VGG16(weights= 'imagenet' ,include_top= False))
    clustering_model.layers[0].trainable = False
    '''
    clustering_model.add(ResNet50(
        include_top=False, pooling='ave'))  #, weights = resnet_weigth_path))
    clustering_model.layers[0].trainable = False

elif weight_type == 'ft':
    num_classes = 9
    base_model = ResNet50
    base_model = base_model(weights='imagenet', include_top=False)

    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(num_classes, activation='softmax')(x)

    clustering_model = Model(inputs=base_model.input, outputs=predictions)
    clustering_model.load_weights(fine_tuned_resnet_weight_path)
Beispiel #6
0
# set the mean subtraction value for each of the data augmentation objects
train_datagen.mean = mean
val_datagen.mean = mean

##################################################################
# FINE TUNING MODEL
import keras
from keras.applications import ResNet50
from keras.layers import Dense, GlobalAveragePooling2D
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras import models, regularizers, layers, optimizers, losses, metrics

conv_base = ResNet50(weights='imagenet',
                     include_top=False,
                     input_shape=(300, 300, 3))

model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dropout(0.5))
model.add(
    layers.Dense(64,
                 activation='relu',
                 kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(class_num, activation="softmax"))

for layer in conv_base.layers[:]:
    layer.trainable = False
Beispiel #7
0
def resnet():
    model = ResNet50(weights="imagenet")
    graph = tf.get_default_graph()
    return model, graph
# the data for training and the remaining 25% for testing
(trainX, testX, trainY, testY) = train_test_split(data,
                                                  labels,
                                                  test_size=0.20,
                                                  random_state=10,
                                                  shuffle=True)

# Resnet initialisation with imagenet

img_height, img_width = 224, 224
num_classes = 2
input_shape = (img_height, img_width, 3)
#base_model=ResNet50(weights='imagenet',include_top=False,input_shape= (img_height,img_width,3)) #imports the mobilenet model and discards the

restnet = ResNet50(include_top=False,
                   weights='imagenet',
                   input_shape=(img_height, img_width, 3))

output = restnet.layers[-1].output
output = keras.layers.Flatten()(output)

preds = Dense(num_classes, activation='softmax')(
    output)  #final layer with softmax activatio

model = Model(inputs=restnet.input, outputs=preds)

# Freeze the layers except the last 4 layers
for layer in restnet.layers[:-3]:
    layer.trainable = False

#model.summary()
from keras.models import Model
from keras.layers import Dense
from keras.layers import Flatten, Dropout
from keras import regularizers
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import load_img
from keras.utils.vis_utils import plot_model
from keras.preprocessing.image import img_to_array
from keras.applications.resnet50 import decode_predictions
import keras.backend as K
import numpy as np

# load model without classifier layers
model = ResNet50(weights="imagenet",
                 include_top=False,
                 input_shape=(224, 224, 3))
# add new classifier layers
x = model.output
x = MaxPooling2D()(x)
for layer in model.layers:
    layer.trainable = False
x = Dense(units=256,
          activation="relu",
          kernel_regularizer=regularizers.l2(0.5))(x)
x = Dropout(0.4)(x)
x = Dense(units=256,
          activation="relu",
          kernel_regularizer=regularizers.l2(0.5))(x)
x = Dropout(0.4)(x)
x = Flatten()(x)
Beispiel #10
0
import argparse
from keras.applications import VGG16, VGG19, ResNet50, InceptionV3

## construct arguments
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--include_top", type=int, default=1, \
        help="1/-1 indicates whether to include the head of neural network or not")
parser.add_argument("-m", "--model", type=str, default="vgg16", \
        help="which model model to inspect")
args = vars(parser.parse_args())

networkBanks = {
        "vgg16" : VGG16(weights="imagenet", include_top=args["include_top"] > 0),
        "vgg19" : VGG19(weights="imagenet", include_top=args["include_top"] > 0),
        "resnet50" : ResNet50(weights="imagenet", \
                include_top=args["include_top"] > 0),
        "inceptionv3" : InceptionV3(weights="imagenet", \
                include_top=args["include_top"] > 0),
        "shallownet" : ShallowNet.build(height=28, width=28, depth=3, classes=10),
        "lenet" : LeNet.build(height=28, width=28, depth=3, classes=10),
        "minivgg" : MiniVGGNet.build(height=28, width=28, depth=3, classes=10),
        #"kfer_lenet" : KFER_LeNet.build(height=48, width=48, depth=3, classes=7),
        }

## loading network
print("[INFO] loading network =", args["model"])
model = networkBanks[args["model"]]

# inspect layers
for i, layer in enumerate(model.layers):
    print("[INFO] {}\t{}".format(i, layer.__class__.__name__))
Beispiel #11
0
# In[ ]:

import tensorflow

# In[ ]:

from keras.applications import ResNet50

# In[ ]:

img_rows = 224
img_cols = 224

#Loads the ResNet50 model
ResNet50 = ResNet50(weights='imagenet',
                    include_top=True,
                    input_shape=(img_rows, img_cols, 3))

# In[ ]:

# Print Layers
for (i, layer) in enumerate(ResNet50.layers):
    print(str(i) + " " + layer.__class__.__name__, layer.trainable)

# In[ ]:

from keras.applications import ResNet50

img_rows = 224
img_cols = 224
argument_parser.add_argument(
    '-c',
    '--confidence',
    type=float,
    default=0.5,
    help='Minimum probability to filter weak detections.')
arguments = vars(argument_parser.parse_args())

INPUT_SIZE = (350, 350)
PYRAMID_SCALE = 2
WINDOW_STEP = 32
ROI_SIZE = (224, 224)
BATCH_SIZE = 64

print('[INFO] Loading network...')
model = ResNet50(weights='imagenet', include_top=True)

labels = dict()

original = cv2.imread(arguments['image'])
(h, w) = original.shape[:2]

resized = cv2.resize(original, INPUT_SIZE, interpolation=cv2.INTER_CUBIC)

batch_rois = None
batch_locations = list()

print('[INFO] Detecting objects...')
start = time.time()

for image in image_pyramid(resized, scale=PYRAMID_SCALE, min_size=ROI_SIZE):
Beispiel #13
0
    batch_size=batch_size_training,
    class_mode='categorical')

print('flag 1.................................................................')

validation_generator = data_generator.flow_from_directory(
    os.path.join(cur_dir, 'concrete_data_week3/valid'),
    target_size=(image_resize, image_resize),
    batch_size=batch_size_training,
    class_mode='categorical')

model = Sequential()

model.add(ResNet50(
    include_top=False,
    pooling='avg',
    weights='imagenet',
    ))

model.add(Dense(num_classes, activation='softmax'))

model.layers

model.layers[0].layers

#Since the ResNet50 model has already been trained, then we want to tell our model not to bother with training the ResNet part, but to train only our dense output layer. To do that, we run the following.
model.layers[0].trainable = False

model.summary()

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Beispiel #14
0
from keras import backend as K
from keras.models import Model
from keras.callbacks import EarlyStopping
from keras.models import load_model

K.set_image_dim_ordering('th')

WEIGHTS_PATH = 'vgg16_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
img_width, img_height = 200, 200

#model = VGG16(include_top=False, weights='imagenet')

input_tensor = Input(shape=(3, img_width,
                            img_height))  # 当使用不包括top的VGG16时,要指定输入的shape,否则会报错
model = ResNet50(include_top=False, weights=None, input_tensor=input_tensor)
print('Model loaded.')
model.load_weights('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')

x = model.output
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(10, activation='softmax')(x)

model2 = Model(inputs=model.input, outputs=x)

#model2 = load_model('mstar.h5')

for layer in model2.layers[:
                           45]:  # set the first 11 layers(fine tune conv4 and conv5 block can also further improve accuracy
Beispiel #15
0
def load_model():
    # load the pre-trained Keras model (here we are using a model
    # pre-trained on ImageNet and provided by Keras, but you can
    # substitute in your own networks just as easily)
    global model
    model = ResNet50(weights="imagenet")
def CNN():
    resnet = ResNet50(include_top=True, weights="imagenet")
    last = resnet.layers[-2].output
    model_resnet = Model(inputs=resnet.input, outputs=last)
    return model_resnet
Beispiel #17
0
# import the necessary packages
import tensorflow as tf
from keras.applications import ResNet50
from keras.preprocessing.image import img_to_array
from keras.applications import imagenet_utils
from PIL import Image
import numpy as np
from flask import Flask, render_template, request, redirect, flash
import io

# initialize our Flask application and the Keras model
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
app.secret_key = r'_5#y2L"F4Q8zsdfsdfec]/'
model = ResNet50(weights="imagenet")
graph = tf.get_default_graph()

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


def prepare_image(image, target):
    # if the image mode is not RGB, convert it
    if image.mode != "RGB":
        image = image.convert("RGB")
Beispiel #18
0
import cv2
from keras.models import load_model
import numpy as np
from keras.applications import ResNet50
from keras.optimizers import Adam
from keras.layers import Dense, Flatten, Input, Convolution2D, Dropout, LSTM, TimeDistributed, Embedding, Bidirectional, Activation, RepeatVector, Concatenate
from keras.models import Sequential, Model
from keras.utils import np_utils
from keras.preprocessing import image, sequence
import cv2
from keras.preprocessing.sequence import pad_sequences
from tqdm import tqdm

from keras.applications import ResNet50
resnet = ResNet50(include_top=False,
                  weights='imagenet',
                  input_shape=(224, 224, 3),
                  pooling='avg')

vocab = np.load('vocab.npy', allow_pickle=True)
vocab = vocab.item()
inv_vocab = {v: k for k, v in vocab.items()}

embedding_size = 128
max_len = 40
vocab_size = len(vocab)

image_model = Sequential()

image_model.add(Dense(embedding_size, input_shape=(2048, ), activation='relu'))
image_model.add(RepeatVector(max_len))
Beispiel #19
0
def load_model():
    global model
    model = ResNet50(weights="imagenet")
    global graph
    graph = tf.get_default_graph()
import numpy as np
import PIL.ExifTags
import PIL.Image
import requests
from dateutil.parser import parse
from keras.applications import ResNet50, imagenet_utils
from keras.preprocessing.image import img_to_array
from lxml import etree
from pykml.factory import KML_ElementMaker as KML
from textblob import TextBlob

KML_POPUP_WIDTH = 400
REVERSE_GEOCODE_URL = 'https://nominatim.openstreetmap.org/reverse'
SLEEP_BEFORE_REQUEST = 2
MODEL = ResNet50(weights="imagenet")
OUTPUT_FILE = 'photos.kml'

parser = argparse.ArgumentParser(description='Create placemarks from photos and export to KML for Google Earth.')
parser.add_argument('--folder', action='store', dest='folder', help='Pick a full path to geotagged image folder.')
parser.add_argument('--language', action='store', dest='language', default='en', help='Pick a language code.')


locations = []


def prepare_image(image, target):
    if image.mode != 'RGB':
        image = image.convert('RGB')
    image = image.resize(target)
    image = img_to_array(image)
Beispiel #21
0
# initiate data augmentor for trainval set
trainvalGen = HDF5DatasetGenerator(
    TRAINVAL_HDF5,
    BATCH_SIZE,
    # RESIZE the org image => validate/test on the whole image
    # substract mean, convert to keras array
    #preprocessors=[pp, mp, iap],
    #preprocessors=[pp, iap],
    #preprocessors=[sp, iap],
    preprocessors=[aap, mp, iap],
    classes=2,
)

## perform model surgery, load ResNet50 network without head layers, explicitly define input_tensor
x = Input(shape=(224, 224, 3))
backbone = ResNet50(weights="imagenet", include_top=False, input_tensor=x)
# FREEZE the backbone & train the new head layers for 10-30 epochs
for layer in backbone.layers:
    layer.trainable = False

head = backbone.output
head = BatchNormalization(axis=-1)(head)
head = GlobalAveragePooling2D()(head)
head = Dense(NUM_CLASSES, activation="softmax")(head)
model = Model(inputs=backbone.input, outputs=head)

print("[INFO] plot model architecture...")
arch_path = os.path.join(OUTPUT_PATH, "resnet50_new_head.png")
plot_model(model, to_file=arch_path, show_shapes=True)

## initalize an optimizer & compile model & train NN
from keras.applications.vgg16 import preprocess_input, decode_predictions
import prologue
from mpl_toolkits.axes_grid1 import ImageGrid

data_dir = r"C:\\Users\\mfajc\\Kaggle\\DogBreeds"
#data_dir = r"/home/ifajcik/kaggle/dog_breed_classification/dogbreed_data"
labels, _, _, _, _ = prologue.init(data_dir)
NUM_CLASSES = 16
# plot image figure via ImageGrid
fig = plt.figure(1, figsize=(16, 16))
j = int(np.sqrt(NUM_CLASSES))
i = int(np.ceil(NUM_CLASSES / j))
grid = ImageGrid(fig, 111, nrows_ncols=(i, j), axes_pad=0.05)

#Pretrained resnet
model = ResNet50(weights='imagenet')

for i, (img_id, breed) in enumerate(labels.loc[labels['rank'] == 1,
                                               ['id', 'breed']].values):
    ax = grid[i]
    img = prologue.read_img(data_dir, img_id, 'train', (224, 224))
    x = preprocess_input(img.copy())
    ax.imshow(img / 255.)
    x = np.expand_dims(img, axis=0)
    preds = model.predict(x)
    _, imagenet_class_name, prob = decode_predictions(preds, top=1)[0][0]
    ax.text(10,
            180,
            'ResNet50: %s (%.2f)' % (imagenet_class_name, prob),
            color='w',
            backgroundcolor='k',
Beispiel #23
0
# First we need to create a model structure
# input layer
image_input = Input(shape=img_train.shape[1:], name="image_input")

if CNN == "IV3":
    # Inception V3 layer with pre-trained weights from ImageNet
    # base_iv3_model = InceptionV3(include_top=False, weights="imagenet")
    base_iv3_model = InceptionV3(weights="imagenet")
    # Inception V3 output from input layer
    x = base_iv3_model(image_input)
    # flattening it #why?
    # flat_iv3 = Flatten()(output_vgg16)
elif CNN == "RN50":
    # ResNet50 layer with pre-trained weights from ImageNet
    base_rn50_model = ResNet50(weights="imagenet")
    # ResNet50 output from input layer
    x = base_rn50_model(image_input)
elif CNN == "Xception":
    # Xception layer with pre-trained weights from ImageNet
    base_xp_model = Xception(weights="imagenet")
    # Xception output from input layer
    x = base_xp_model(image_input)


# We stack dense layers and dropout layers to avoid overfitting after that
x = Dense(1000, activation="relu")(x)
x = Dropout(0.2)(x)
x = Dense(1000, activation="relu")(x)
x = Dropout(0.2)(x)
# x = Dense(240, activation="relu")(x)
Beispiel #24
0
import numpy as np
import tensorflow as tf

from PIL import Image
from keras.applications.resnet50 import preprocess_input
from keras.applications import ResNet50
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.densenet import decode_predictions

model = ResNet50()
graph = tf.get_default_graph()

def read_and_prep_image(bytes):
    im = Image.open(bytes)
    im = im.resize((224, 224))
    img_array = np.array([img_to_array(im)])
    return preprocess_input(img_array)

def predict(bytes):
    image_data = read_and_prep_image(bytes)

    with graph.as_default():
        preds = model.predict(image_data)
        return decode_predictions(preds, top=1)[0]
Beispiel #25
0
def train(train_path, valid_path, model_save_path):
    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # 给gpu排序
    os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"  # 选择gpu设备

    if not sys.warnoptions:
        warnings.simplefilter("ignore")
        os.environ["PYTHONWARNINGS"] = "ignore"  # 过滤警告提醒

    config = tf.ConfigProto(device_count={
                            "CPU": 16}, inter_op_parallelism_threads=1, intra_op_parallelism_threads=1)  # 设置CPU最大数量为16
    config.gpu_options.allocator_type = 'BFC'  # 使用BFC算法 BFC是分配、释放内存,碎片管理的算法
    config.gpu_options.allow_growth = True  # 程序按需申请内存

    with tf.Session(config=config):
        pool = multiprocessing.Pool(processes=16)  # 创建16个进程
        img_datagen = ImageDataGenerator(  # 图像预处理
            rescale=1./255,             #对图像进行放大、缩小
            rotation_range=2,           #随机旋转
            width_shift_range=0.2,      #沿着水平、垂直方向为变化范围进行平移
            height_shift_range=0.2,
            zoom_range=[0.9, 1.2],      #按比例随机缩放图像尺寸
            fill_mode='nearest',        #填充像素
            dim_ordering='tf',
            pool=pool
        )
        train_generator = img_datagen.flow_from_directory(          #以文件夹路径为参数,生成经过数据提升/归一化数据,在一个无限循环中产生无限的batch数据
            train_path,
            target_size=(299, 299),
            batch_size=batch_size,
            class_mode='categorical'
        )
        valid_generator = img_datagen.flow_from_directory(
            valid_path,
            target_size=(299, 299),
            batch_size=batch_size,
            class_mode='categorical'
        )

        early_stopping = EarlyStopping(                     #防止模型过拟合,当网络在训练集表现地越来越好,loss表现地越来越差地时候
            monitor='val_acc', mode='max', min_delta=0.01, patience=8, verbose=1
        )

        auto_lr = ReduceLROnPlateau(                        #当标准评估停止提升时,降低学习速率
            monitor='val_loss', factor=0.1, patience=2, verbose=1, mode='auto', eosilon=0.0001, cooldown=0, min_lr=0
        )

        callback_checkpoint = ModelCheckpoint(filepath=model_save_path + "{epoch:03d}" + model_name,            #在每个训练期之后保存模型
                                              monitor='val_loss',
                                              mode='min',
                                              verbose=1,
                                              save_weights_only=False,
                                              save_best_only=False)

        model_path = ''
        if os.path.exists(model_path):
            model = load_model(model_path)
        else:
            with tf.device("/cpu:0"):
                base_model = ResNet50(                  #加载ResNet50的Image Net的预训练模型
                    include_top=True, weights='imagenet', input_tensor=None, input_shape=None
                )
                base_model = Model(                     #设置模型的输入与输出
                    inputs=[base_model.input], outputs=[base_model.get_layer('avg_pool').output], name='InceptionResNetV2'
                )

            img = Input(shape=(299, 299, 3), name='img')
            feature = base_model(img)

            classes_count = len(os.listdir(train_path))
            category_predict = Dense(classes_count, activation='softmax', name='ctg_out')(
                Dropout(0.5)(feature)
            )

            model = Model(inputs=img, outputs=category_predict)
            
            #设置冻结层
            for layers in base_model.layers[:20]:
                layers.trainable = False
            for layers in base_model.layers[20:]:
                layers.trainable = True
            
            print('---------base model layers---------','{}\t{}'.format(datetime.now().strftime('%m:%d %H-%M-%S'),len(base_model.layers)), sep='\n')
            print('---------train generator count---------','{}\t{}'.format(datetime.now().strftime('%m:%d %H-%M-%S'),len(train_generator.filenames)), sep='\n')
            
            optimizer = keras.optimizers.Adadelta()
            model.compile(optimizer=optimizer,
                            loss = {
                                'ctg_out':'categorical_crossentropy'
                            },
                            metrics=['accuracy'])
            each_epoch_callback = CustomCallback()

            model.fit_generator(train_generator,
                                steps_per_epoch=len(train_generator.filenames) // (3*batch_size),
                                epochs=epochs,
                                verbose=1,
                                workers=16,
                                validation_data=valid_generator,
                                validation_steps=len(valid_generator.filenames)//(batch_size),
                                callbacks=[callback_checkpoint, early_stopping, auto_lr, each_epoch_callback]
                                )
def load_model():
    """load the pre-trained Keras model (here we are using a model"""
    global model
    model = ResNet50(weights="imagenet")
Beispiel #27
0
        preprocessing_function=preproc_fn)
    idg_args = {
        'target_size': target_size,
        'color_mode': 'rgb',
        'class_mode': None,
        'batch_size': batch_size,
        'shuffle': True
    }
    data_provider = datagen.flow_from_h5file(db_path, **idg_args)
    num_classes = data_provider.num_classes
    printmsg("Found", data_provider.tot_samples, "images belonging to",
             data_provider.num_classes, "classes.")
    # Build network
    if args["arch"] == 'resnet':
        model = ResNet50(include_top=False,
                         weights='imagenet',
                         input_shape=(target_size[0], target_size[1], 3),
                         pooling='avg')
    elif args["arch"] == 'alexnet':
        model = AlexNet(include_top=False,
                        weights='imagenet',
                        input_shape=(3, target_size[0], target_size[1]),
                        trainable=False)
    else:
        raise ValueError("No such architecture implemented")
    model.compile(optimizer='rmsprop', loss='mse')
    n_logits = model.output_shape[1]
    printmsg("Network created -", n_logits, "logits")
    # Start writing the databases
    with h5py.File(filename, read_mode) as f:
        printmsg("Output file", filename, "opened")
Beispiel #28
0
def load_model():
    global model
    model = ResNet50(weights="imagenet")
Beispiel #29
0
from sklearn.metrics import confusion_matrix
import itertools
%matplotlib inline
from tensorflow.python.keras.preprocessing import image
from tensorflow.python.keras.applications.inception_v3 import *
import numpy as np
from keras.applications import ResNet50

train_path = 'Path_for_Images'
test_path = 'Path_for_Images'

train_batches = ImageDataGenerator().flow_from_directory(train_path,target_size=(197,197),classes=['positive','negative'],batch_size=1)
test_batches = ImageDataGenerator().flow_from_directory(test_path,target_size=(197,197),classes=['positive','negative'],batch_size=1)

images, train_labels = next(train_batches)
resnet_model = ResNet50(include_top=True,weights='imagenet')
resnet_model = keras.applications.resnet50.ResNet50(weights=None,classes=2,input_shape=(197,197,3))
resnet_model.summary()

for layer in resnet_model.layers:
    layer.trainable = False
resnet_model.compile(Adam(lr=0.0001),loss='categorical_crossentropy',metrics=['accuracy'])
resnet_model.fit_generator(train_batches,epochs=5)


test_images, test_labels = next(test_batches)
predict = resnet_model.predict_generator(test_batches)

test_batches = ImageDataGenerator().flow_from_directory(test_path,target_size=(197,197),classes=['positive','negative'],batch_size=564)
test_images, test_labels = next(test_batches)
counter = 0
Beispiel #30
0
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Activation, Flatten, MaxPool2D, BatchNormalization
from keras.optimizers import Adam
from keras.applications import ResNet50
from keras.applications import resnet50 as resnet50
from keras.preprocessing import image

#%%

# new_model = ResNet50(
#     include_top = False,
#     weights = 'imagenet',
#     intput_shape = [32,32,3]
# )

new_model = ResNet50()

#%%

# if we need to resize the image:
import skimage.transform
# for each image:
# new_image = skimage.transform.resize(image, (299, 299), mode='constant')
# X_train_reshape.append(new_image)

new_image = skimage.transform.resize(train_images[6], (224, 224),
                                     mode='constant')

plt.hist(new_image.flatten())
np.max(new_image.flatten())
np.min(new_image.flatten())