Exemplo n.º 1
0
# -*- coding: utf-8 -*-
"""
Created on Tue Nov  5 16:32:16 2019

@author: luket
"""
from tensorflow.keras.applications.vgg16 import VGG16
model = VGG16(weights='imagenet', include_top=False)

import IPython.display as display
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import pathlib
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.constraints import max_norm
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics import classification_report, confusion_matrix
#small code chunk to split folders into train/val/test
#import split_folders
# Split with a ratio.
# To only split into training and validation set, set a tuple to `ratio`, i.e, `(.8, .2)`.
#split_folders.ratio('C:/Users/luket/Desktop/CS_251_DogProject/root_data/Images/Images', output="output", seed=1337, ratio=(.8, .1, .1)) # default values

#create image data generators for training and test sets
shift = 0.2
IMAGE_SIZE = 250
BATCH_SIZE = 64
import numpy as np
import os
import tensorflow as tf
from tensorflow.keras.layers import Dense,Embedding,Input,Flatten,GRU
from tensorflow.keras import Model
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences     

import csv
encoder_pre_model = VGG16(include_top=True, weights='imagenet')

encoder_output_layer = encoder_pre_model.get_layer('fc2')
encoder_output = encoder_output_layer.output
encoder_output = Dense(256, activation=tf.nn.tanh)(encoder_output)

encoder_model = Model(encoder_pre_model.input, encoder_output)
encoder_model = Model(encoder_pre_model.input, encoder_output)

for n in range(len(encoder_model.layers)):
    encoder_model.layers[n-1].trainable = False
encoder_model.summary()
image_dir = r'C:\ml\image captioning\31296_39911_bundle_archive\flickr30k_images\flickr30k_images'
caption_csv = r'C:\ml\image captioning\31296_39911_bundle_archive\flickr30k_images\results.csv'
imgcap = {}
captions = []
uni_img = []
uni_cap = []
c=0
d=0
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
import numpy as np

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus, cpus)

tf.config.experimental.set_virtual_device_configuration(
    gpus[0],
    [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=4096)])

batch_size = 32

model = VGG16(input_shape=(150, 150, 3),
              include_top=True,
              weights=None,
              classes=10)
from tensorflow.keras.optimizers import RMSprop

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(lr=0.0001),
              metrics=['acc'])
'''
loadModel = "./10/10_model_garbage_vgg16.h5"
model = tf.keras.models.load_model(loadModel)
'''
# model.summary()
base = './10/dataset/'
train_datagen = ImageDataGenerator(rescale=1. / 255.,
                                   rotation_range=40,
                                   width_shift_range=0.2,
Exemplo n.º 4
0
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras import models
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
import numpy as np

base_model = VGG16(weights='imagenet', include_top=True)

print(base_model)
''' result
<tensorflow.python.keras.engine.functional.Functional object at 0x000001330636DF48>
'''

for i, layer in enumerate(base_model.layers):
    print(i, layer.name, layer.output_shape)
'''result
0 input_1 [(None, 224, 224, 3)]
1 block1_conv1 (None, 224, 224, 64)
2 block1_conv2 (None, 224, 224, 64)
3 block1_pool (None, 112, 112, 64)
4 block2_conv1 (None, 112, 112, 128)
5 block2_conv2 (None, 112, 112, 128)
6 block2_pool (None, 56, 56, 128)
7 block3_conv1 (None, 56, 56, 256)
8 block3_conv2 (None, 56, 56, 256)
9 block3_conv3 (None, 56, 56, 256)
10 block3_pool (None, 28, 28, 256)
11 block4_conv1 (None, 28, 28, 512)
12 block4_conv2 (None, 28, 28, 512)
13 block4_conv3 (None, 28, 28, 512)
14 block4_pool (None, 14, 14, 512)
def scheduler(epoch):
    if epoch < 10:
        return 0.001
    else:
        return 0.001 * tf.math.exp(0.1 * (10 - epoch))


callbacks = [
    tf.keras.callbacks.EarlyStopping(patience=8, monitor='val_loss'),
    tf.keras.callbacks.TensorBoard(log_dir='./logs'),
    tf.keras.callbacks.LearningRateScheduler(scheduler)
]

base_model0 = VGG16(weights='imagenet',
                    include_top=False,
                    input_shape=(224, 224, 3))
base_model1 = VGG19(weights='imagenet',
                    include_top=False,
                    input_shape=(224, 224, 3))
base_model2 = MobileNet(weights='imagenet',
                        include_top=False,
                        input_shape=(224, 224, 3))
base_model3 = InceptionV3(weights='imagenet',
                          include_top=False,
                          input_shape=(224, 224, 3))
base_model4 = ResNet50(weights='imagenet',
                       include_top=False,
                       input_shape=(224, 224, 3))

base_models = [base_model0, base_model1, base_model2, base_model3, base_model4]
Exemplo n.º 6
0
                       required=True,
                       help="Path to the index file")
argParser.add_argument("-q",
                       "--query",
                       required=True,
                       help="Path to the query image")
argParser.add_argument("-r",
                       "--result_path",
                       required=True,
                       help="Path to the result path")
args = vars(argParser.parse_args())

if os.path.isdir("vgg16"):
    model = keras.models.load_model("vgg16")
else:
    model = VGG16(weights="imagenet", include_top=False)

searcher = Searcher(args["index"])

img_path = args["query"]
img = image.load_img(img_path, target_size=(244, 244))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)

query_features = model.predict(img_array)
features_numpy = np.array(query_features)

(dist, img_ids) = searcher.search(features_numpy.flatten(), 10)

query_img = cv2.imread(img_path)
query_resized = cv2.resize(query_img, (720, 480))
Exemplo n.º 7
0
# データのロード
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=42)
#list型をnumpy配列に変換
X_train = np.array(X_train)
X_test = np.array(X_test)
#正解ラベルをOnne-hot形式に変換
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

input_tensor = Input(shape=(150, 150, 1))
vgg16 = VGG16(include_top=False,
              weights=None,
              input_tensor=input_tensor,
              input_shape=(100, 100, 1))

# モデルの定義
model = Sequential()
model.add(Flatten(input_shape=vgg16.output_shape[1:]))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.25))
model.add(BatchNormalization())
model.add(Dense(7, activation='softmax'))

model = Model(inputs=vgg16.input, outputs=model(vgg16.output))

for layer in model.layers[:19]:
Exemplo n.º 8
0
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
    X, Y, test_size=0.2, random_state=42, stratify=Y)

x_train, x_val, y_train, y_val = train_test_split(
    x_train, y_train, test_size=0.2, random_state=42
)


from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input

model = Sequential()

model.add(VGG16(weights='imagenet', include_top=False, input_shape=(64,64,3)))
for layer in model.layers:
     layer.trainable = False
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(29, activation='softmax'))

model.add(VGG16(weights='imagenet', include_top=False, input_shape=(64,64,3)))
for layer in model.layers:
     layer.trainable = False
model.summary()

from keras.optimizers import Adam,RMSprop,Adadelta,Nadam,SGD
from keras.callbacks import EarlyStopping, ReduceLROnPlateau,ModelCheckpoint
es=EarlyStopping(patience=20, verbose=1, monitor='val_loss',restore_best_weights = True)
myDir ="/content/train/*.png"
labels = load_labels(myDir)
data = load_data(myDir)
Labels = tensorflow.keras.utils.to_categorical(labels,10)

# converting the grey scale image to rgb images for transfer learning
rgb_batch = np.repeat(data[..., np.newaxis], 3, -1)    
print(rgb_batch.shape)

data.shape

"""Loading VGG 16 for transfer learning"""

from tensorflow.keras.applications.vgg16 import VGG16
vgg_conv = VGG16(weights='imagenet' , include_top = False, input_shape=(32,32,3))

for layers in vgg_conv.layers[:-4]:
  layers.trainable = False

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(rgb_batch,Labels,test_size=0.3,random_state=seed_value) 
img_x , img_y = 32 , 32
X_train = X_train.reshape(X_train.shape[0] , img_x , img_y , 3)
X_test = X_test.reshape(X_test.shape[0] , img_x , img_y , 3)
input_shape = (img_x,img_y,1)

y_train.shape

"""CNN model"""
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest',
      cval=5)


validation_datagen = ImageDataGenerator(
      rescale = 1./255)

training_set=training_datagen.flow(X_train,y_train)
test_set=validation_datagen.flow(X_test,y_test)

IMAGE_SIZE=[224,224]
vgg= VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)

  # don't train existing weights\n",
for layer in vgg.layers:
  layer.trainable = False

folders = glob('/content/drive/My Drive/Colab Notebooks/Facial Expressions/*')
x = Flatten()(vgg.output)
prediction=Dense(len(folders), activation='softmax')(x)
model = Model(inputs=vgg.input, outputs=prediction)
model.summary()

model.compile(
  loss='categorical_crossentropy',
  optimizer='adam',
  metrics='accuracy')
Exemplo n.º 11
0
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.layers import Dense, Dropout, Flatten, Input
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import get_file

num_classes = 12  # クラス数

# VGG16 モデルを作成する。
vgg16 = VGG16(include_top=False,
              weights="imagenet",
              input_tensor=Input(shape=(224, 224, 3)))

vgg16.trainable = False  # 重みをフリーズする。

model = Sequential([
    vgg16,
    Flatten(),
    Dense(500, activation="relu"),
    Dropout(0.5),
    Dense(500, activation="relu"),
    Dropout(0.5),
    Dense(num_classes, activation="softmax"),
])

model.summary()

# コンパイル
Exemplo n.º 12
0
            data.append(j)

    data = np.array(data, dtype="float") / 255.0
    return data, labels


def load_svm_relationship():
    file = open('models/SVM_relationships', 'rb')
    return pickle.load(file)


if __name__ == '__main__':
    trainX, trainY = load_data()

    base_model = VGG16(weights='imagenet',
                       include_top=False,
                       input_tensor=Input(shape=(300, 300, 3)),
                       input_shape=(300, 300, 3))

    features = base_model.predict(trainX, batch_size=32, verbose=1)
    features = features.reshape((features.shape[0], 512 * 9 * 9))

    clf_svm = SVC(kernel='linear', probability=True)
    clf_svm.fit(features, trainY)

    testData, testLabels = load_data("test")
    features = base_model.predict(testData, batch_size=32, verbose=1)
    features = features.reshape((features.shape[0], 512 * 9 * 9))

    y_train_pred = clf_svm.predict(features)
    print(accuracy_score(testLabels, y_train_pred))
Exemplo n.º 13
0
    # check if the output directory exists, if not, create it.
    existing_directory("results")
    existing_directory("results/deep_features")
    existing_directory("results/deep_features/" + dataset[ds])

    # start time
    print("[STATUS] start time - {}".format(
        datetime.datetime.now().strftime("%Y-%m-%d %H:%M")))
    start = time.time()

    # create the pretrained models
    # check for pretrained weight usage or not
    # check for top layers to be included or not
    if model_name == "vgg16":
        base_model = VGG16(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('fc1').output)
        image_size = (224, 224)
    elif model_name == "vgg19":
        base_model = VGG19(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('fc1').output)
        image_size = (224, 224)
    elif model_name == "resnet50":
        base_model = ResNet50(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('flatten').output)
        image_size = (224, 224)
    elif model_name == "inceptionv3":
        base_model = InceptionV3(include_top=include_top,
Exemplo n.º 14
0
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from PIL import Image
import warnings
warnings.filterwarnings("ignore")


image_size = [224,224]
data_path = 'Data'
conv = VGG16(input_shape= image_size+[3],weights='imagenet',include_top=False)
conv.output


x = conv.output
x = GlobalAveragePooling2D()(x)


x = Dense(1024,activation='relu')(x)
x = Dense(1024,activation='relu')(x)
x = Dense(512, activation='relu')(x)


pred = Dense(2,activation='softmax')(x)
model = Model(inputs = conv.input,outputs=pred)
Exemplo n.º 15
0
num_classes = len(train_generator.class_indices)

# save the class indices for use in the predictions
np.save('class_indices.npy', train_generator.class_indices)

# calculate the training steps
nb_train_samples = len(train_generator.filenames)
train_steps = int(math.ceil(nb_train_samples / batch_size))

# calculate the validation steps
nb_validation_samples = len(validation_generator.filenames)
validation_steps = int(math.ceil(nb_validation_samples / batch_size))

# create the base pre-trained model
base_model = VGG16(weights='imagenet',
                   include_top=False,
                   input_tensor=Input(shape=(img_width, img_height, 3)))

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
predictions = Dense(num_classes, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional layers
for layer in base_model.layers:
    layer.trainable = False
Exemplo n.º 16
0
# example of using a pre-trained model as a classifier
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.vgg16 import decode_predictions
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D, AveragePooling2D

Xtrain, Xtest, ytrain, ytest = train_test_split(res_img2,
                                                res_label2,
                                                test_size=0.2,
                                                random_state=8)

vgg_model = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(227, 227, 3))

vgg_model.layers
vgg_model.summary()
len(vgg_model.layers)

vgg_model.trainable = False

inp = keras.Input(shape=(227, 227, 3))

x = vgg_model(inp)

# Convert features of shape `base_model.output_shape[1:]` to vectors
#x = keras.layers.GlobalAveragePooling2D()(x)
# A Dense classifier with a single unit (binary classification)
x = keras.layers.Activation('relu')(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(1000, input_shape=(7, 7, 512))(x)
Exemplo n.º 17
0
total_cropped_images_train = np.array(total_cropped_images_train)

# Importing required libraries for construction of model.
from tensorflow import keras
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras import layers

# VGG16 is already trained model. "model1" returns features representations as
# a vector of length 8192 for each images. The final shape of features is (N,8192) 
# where N is the total number of images used for training the model.

model = VGG16(include_top = False, input_shape = (128,128,3))  
output1 = Flatten()(model.layers[-1].output)
model1 = Model(inputs = model.inputs, outputs = output1)
model_svm = Model(inputs = model.inputs, outputs = model.layers[-2].output)

preprocessed_input = preprocess_input(total_cropped_images_train)
ann_features_train = model1.predict(preprocessed_input)

##########################################################################################
#svm_features_train = model_svm.predict(preprocessed_input)
#svm_features_train = tf.keras.layers.GlobalMaxPool2D()(svm_features_train)
#svm_features_train = np.array(svm_features_train)
###########################################################################################

# The features representations returnde by the model1 is fed to model 2 which retures two 
# numbers which are the probabilities of an image being classified as cloudy and clear image.
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import load_img, img_to_array

import os

#creating an object for VGG16 model(pre-trained)

model = VGG16()

for file in os.listdir('sample'):
    print(file)
    full_path = 'sample/' + file

    image = load_img(full_path, target_size=(224, 224))
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)
    y_pred = model.predict(image)
    label = decode_predictions(y_pred, top=2)
    print(label)
    print()
IMAGE_SIZE = [200, 200]

#getting number of files
image_files = glob(train_path + '/*/*.jpg')
valid_image_files = glob(valid_path + '/*/*.jpg')

#number of classes
folders = glob(train_path + '/*')
print(folders)

plt.imshow(image.load_img(np.random.choice(image_files)))
plt.show()

#Load the pre-trained model................
ptm = PretrainedModel(input_shape=IMAGE_SIZE + [3],
                      weights='imagenet',
                      include_top=False)

ptm.trainable = False

#Build the model..................
K = len(folders)
x = Flatten()(ptm.output)
x = Dense(K, activation='softmax')(x)

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

print(model.summary())

#Create a instance of generator..........
gen = ImageDataGenerator(rotation_range=20,
Exemplo n.º 20
0
test_it = datagen.flow_from_directory(test_folder,
                                      target_size=(IMG_SIZE, IMG_SIZE),
                                      class_mode='categorical',
                                      batch_size=batch_size,
                                      shuffle=False)

filepath = os.path.join(output_folder, modelname + ".hdf5")
loss_epoch_file = os.path.join(output_folder, modelname + '.csv')
plt_file = os.path.join(output_folder, modelname + '_plot.png')

print(filepath)
print(loss_epoch_file)

modelGo = VGG16(include_top=True,
                input_shape=(IMG_SIZE, IMG_SIZE, 3),
                weights=None,
                classes=4)
modelGo.load_weights(filepath)
modelGo.compile(loss='categorical_crossentropy',
                optimizer='adam',
                metrics=['accuracy'])

predict = modelGo.predict_generator(test_it)
loss, acc = modelGo.evaluate_generator(test_it)

import math
tsLbl = []
test_it.reset()
count = 0
number_of_examples = len(test_it.filenames)
print("number of examples: {0}".format(number_of_examples))
Exemplo n.º 21
0
def get_vgg_twoeyes(optimizer='adam',
                    model_type='VGG19',
                    fc1_size=1024,
                    fc2_size=512,
                    fc3_size=256):
    kern_init = initializers.glorot_normal()

    img_input_l = Input(shape=(36, 60, 3), name='img_input_L')
    img_input_r = Input(shape=(36, 60, 3), name='img_input_R')
    headpose_input = Input(shape=(2, ), name='headpose_input')

    # create the base pre-trained model
    if model_type == 'VGG19':
        base_model_l = VGG19(input_tensor=img_input_l,
                             weights='imagenet',
                             include_top=False)
        base_model_r = VGG19(input_tensor=img_input_r,
                             weights='imagenet',
                             include_top=False)
    elif model_type == 'VGG16':
        base_model_l = VGG16(input_tensor=img_input_l,
                             weights='imagenet',
                             include_top=False)
        base_model_r = VGG16(input_tensor=img_input_r,
                             weights='imagenet',
                             include_top=False)
    else:
        raise Exception('Unknown model type in get_vgg_twoeyes')

    for layer_L in base_model_l.layers[1:]:
        layer_L.name = 'layer_L_' + layer_L.name
    for layer_R in base_model_r.layers[1:]:
        layer_R.name = 'layer_R_' + layer_R.name

    # add a global spatial average pooling layer
    x_l = base_model_l.output
    x_l = GlobalAveragePooling2D()(x_l)
    x_r = base_model_r.output
    x_r = GlobalAveragePooling2D()(x_r)

    # let's add a fully-connected layer
    x_l = Dense(fc1_size, kernel_initializer=kern_init)(x_l)
    x_l = BatchNormalization()(x_l)
    x_l = Activation('relu')(x_l)

    x_r = Dense(fc1_size, kernel_initializer=kern_init)(x_r)
    x_r = BatchNormalization()(x_r)
    x_r = Activation('relu')(x_r)

    x = concatenate([x_l, x_r])

    x = Dense(fc2_size, kernel_initializer=kern_init)(x)
    x = concatenate([x, headpose_input])

    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dense(fc3_size, activation='relu', kernel_initializer=kern_init)(x)

    gaze_predictions = Dense(2, kernel_initializer=kern_init,
                             name='pred_gaze')(x)

    # this is the model we will train
    model = Model(inputs=[img_input_l, img_input_r, headpose_input],
                  outputs=gaze_predictions)
    model.compile(optimizer=optimizer,
                  loss=angle_loss,
                  metrics=['accuracy', accuracy_angle])
    return model
Exemplo n.º 22
0
    x = preprocess_input(first_audio)
    x = np.expand_dims(x, axis=0)
    first_vect = first_net.predict(x)

    y = preprocess_input(second_audio)
    y = np.expand_dims(y, axis=0)
    second_vect = second_net.predict(y)

    return first_vect, second_vect


data_set, files = get_data_set("dataset")
test_len = round(len(data_set))

first_model_full = VGG16()
second_model_full = VGG16()

first_model = Model(inputs=first_model_full.input,
                    outputs=first_model_full.get_layer('fc2').output)
second_model = Model(inputs=second_model_full.input,
                     outputs=second_model_full.get_layer('fc2').output)

euclideans = []

for i in range(test_len):
    for j in range(i + 1, test_len):

        f1, t1, s1 = spectrogram(data_set[i])
        f2, t2, s2 = spectrogram(data_set[j])
Exemplo n.º 23
0
def Segnet(pretrained=True, tnb_extractor=True):
    """#Building the model

    > Here we first download the VGG16 feature extractor model (and for this purpose we remove the classification block, i.e. the dense layers)

    > After, we build the Segnet encoder model by adding the particular segnet layers to the original VGG16 feature extractor, so that we can take advantage of the pretrained weights from the imagenet dataset.

    > Finally, we build the Segnet decoder and merge it to the encoder to form the complete Segnet model.
    """

    if (pretrained):
        feature_extractor = VGG16(weights='imagenet',
                                  include_top=False,
                                  input_shape=(config.IMAGE_HEIGHT,
                                               config.IMAGE_WIDTH,
                                               config.CHANNELS))
        feature_extractor.trainable = tnb_extractor
    else:
        feature_extractor = VGG16(include_top=False,
                                  input_shape=(config.IMAGE_HEIGHT,
                                               config.IMAGE_WIDTH,
                                               config.CHANNELS))
        feature_extractor.trainable = True

    pool_size = (2, 2)
    """# Encoder"""

    model_input = Input(shape=(config.IMAGE_WIDTH, config.IMAGE_HEIGHT,
                               config.CHANNELS))
    x = (feature_extractor.get_layer('block1_conv1'))(model_input)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block1_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    pool_1, mask_1 = MaxPoolingWithArgmax2D(pool_size)(x)

    x = (feature_extractor.get_layer('block2_conv1'))(pool_1)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block2_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    pool_2, mask_2 = MaxPoolingWithArgmax2D(pool_size)(x)

    x = (feature_extractor.get_layer('block3_conv1'))(pool_2)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block3_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block3_conv3')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    pool_3, mask_3 = MaxPoolingWithArgmax2D(pool_size)(x)

    x = (feature_extractor.get_layer('block4_conv1'))(pool_3)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block4_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block4_conv3')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    pool_4, mask_4 = MaxPoolingWithArgmax2D(pool_size)(x)

    x = (feature_extractor.get_layer('block5_conv1'))(pool_4)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block5_conv2')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = feature_extractor.get_layer('block5_conv3')(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    pool_5, mask_5 = MaxPoolingWithArgmax2D(pool_size)(x)
    """### Decoder"""

    unpool_1 = MaxUnpooling2D(pool_size)([pool_5, mask_5])

    x = (Conv2D(512, (3, 3), padding="same"))(unpool_1)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(512, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(512, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    unpool_2 = MaxUnpooling2D(pool_size)([x, mask_4])

    x = (Conv2D(512, (3, 3), padding="same"))(unpool_2)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(512, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(256, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    unpool_3 = MaxUnpooling2D(pool_size)([x, mask_3])

    x = (Conv2D(256, (3, 3), padding="same"))(unpool_3)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(256, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(128, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    unpool_4 = MaxUnpooling2D(pool_size)([x, mask_2])

    x = (Conv2D(128, (3, 3), padding="same"))(unpool_4)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = (Conv2D(64, (3, 3), padding="same"))(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    unpool_5 = MaxUnpooling2D(pool_size)([x, mask_1])

    x = (Conv2D(64, (3, 3), padding="same"))(unpool_5)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = (Conv2D(5, (1, 1), padding="valid"))(x)
    x = BatchNormalization()(x)
    #x = Reshape((config.IMAGE_WIDTH * config.IMAGE_HEIGHT, config.NUM_CLASSES), input_shape=(config.IMAGE_WIDTH, config.IMAGE_HEIGHT, config.NUM_CLASSES))(x)

    model_output = Activation('softmax')(x)

    model = Model(inputs=model_input, outputs=model_output, name="SegNet")

    model.summary()

    return model
def unet(input_shape=(320, 320, 3), weights='imagenet', num_cls=32):
    #加载ImageNet预训练权重
    vgg16_model = VGG16(input_shape=input_shape,
                        weights=weights,
                        include_top=False)

    # 编码过程VGG16经过4次降采样得到 20*20的特征图
    block4_pool = vgg16_model.get_layer('block4_pool').output
    block5_conv1 = Conv2D(1024,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block4_pool)
    block5_conv2 = Conv2D(1024,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block5_conv1)
    block5_drop = Dropout(0.5)(block5_conv2)

    # 第1次上采样解码,与VGG第3次降采样编码的结果融合得到 40*40的特征图
    block6_up = Conv2D(512,
                       2,
                       activation='relu',
                       padding='same',
                       kernel_initializer='he_normal')(
                           UpSampling2D(size=(2, 2))(block5_drop))
    block6_merge = Concatenate(axis=3)(
        [vgg16_model.get_layer('block4_conv3').output, block6_up])
    block6_conv1 = Conv2D(512,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block6_merge)
    block6_conv2 = Conv2D(512,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block6_conv1)
    block6_conv3 = Conv2D(512,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block6_conv2)

    # 第2次上采样解码,与VGG第2次降采样编码的结果融合得到 80*80的特征图
    block7_up = Conv2D(256,
                       2,
                       activation='relu',
                       padding='same',
                       kernel_initializer='he_normal')(
                           UpSampling2D(size=(2, 2))(block6_conv3))
    block7_merge = Concatenate(axis=3)(
        [vgg16_model.get_layer('block3_conv3').output, block7_up])
    block7_conv1 = Conv2D(256,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block7_merge)
    block7_conv2 = Conv2D(256,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block7_conv1)
    block7_conv3 = Conv2D(256,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block7_conv2)

    # 第3次上采样解码,与VGG第1次降采样编码的结果融合得到 160*160的特征图
    block8_up = Conv2D(128,
                       2,
                       activation='relu',
                       padding='same',
                       kernel_initializer='he_normal')(
                           UpSampling2D(size=(2, 2))(block7_conv3))
    block8_merge = Concatenate(axis=3)(
        [vgg16_model.get_layer('block2_conv2').output, block8_up])
    block8_conv1 = Conv2D(128,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block8_merge)
    block8_conv2 = Conv2D(128,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block8_conv1)

    # 第4次上采样解码,与VGG降采样之前编码的结果融合得到 320*320的特征图
    block9_up = Conv2D(64,
                       2,
                       activation='relu',
                       padding='same',
                       kernel_initializer='he_normal')(
                           UpSampling2D(size=(2, 2))(block8_conv2))
    block9_merge = Concatenate(axis=3)(
        [vgg16_model.get_layer('block1_conv2').output, block9_up])
    block9_conv1 = Conv2D(64,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block9_merge)
    block9_conv2 = Conv2D(64,
                          3,
                          activation='relu',
                          padding='same',
                          kernel_initializer='he_normal')(block9_conv1)
    block10_conv1 = Conv2D(64,
                           3,
                           activation='relu',
                           padding='same',
                           kernel_initializer='he_normal')(block9_conv2)

    # 将最后的特征图映射到像素分类空间中去,卷积输出通道数为像素类别数
    block10_conv2 = Conv2D(num_cls, 1, activation='sigmoid')(block10_conv1)

    model = Model(inputs=vgg16_model.input, outputs=block10_conv2)
    return model
Exemplo n.º 25
0
load_weights_from_frcnn = False
hyper_params = helpers.get_hyper_params(nms_topn=10)

VOC_test_data, VOC_info = helpers.get_dataset("voc/2007", "test")
labels = helpers.get_labels(VOC_info)
# We add 1 class for background
hyper_params["total_labels"] = len(labels) + 1
# If you want to use different dataset and don't know max height and width values
# You can use calculate_max_height_width method in helpers
max_height, max_width = helpers.VOC["max_height"], helpers.VOC["max_width"]
VOC_test_data = VOC_test_data.map(lambda x : helpers.preprocessing(x, max_height, max_width))

padded_shapes, padding_values = helpers.get_padded_batch_params()
VOC_test_data = VOC_test_data.padded_batch(batch_size, padded_shapes=padded_shapes, padding_values=padding_values)

base_model = VGG16(include_top=False)
if hyper_params["stride"] == 16:
    base_model = Sequential(base_model.layers[:-1])
rpn_model = rpn.get_model(base_model, hyper_params)

frcnn_model_path = helpers.get_model_path("frcnn", hyper_params["stride"])
rpn_model_path = helpers.get_model_path("rpn", hyper_params["stride"])
model_path = frcnn_model_path if load_weights_from_frcnn else rpn_model_path
rpn_model.load_weights(model_path, by_name=True)

for image_data in VOC_test_data:
    img, gt_boxes, gt_labels = image_data
    input_img, anchors = rpn.get_step_data(image_data, hyper_params, preprocess_input, mode="inference")
    rpn_bbox_deltas, rpn_labels = rpn_model.predict_on_batch(input_img)
    #
    anchors_shape = tf.shape(anchors)
plt.title("model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","loss","Validation Loss"])
plt.show()

###---vgg16---final----4th method-----------

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Flatten, Dense, Dropout
from tensorflow.keras.applications import VGG16

IMAGE_SIZE = [380, 380]  # we will keep the image size as (380,380). You can increase the size for better results. 
num_classes=6
# loading the weights of VGG16 without the top layer. These weights are trained on Imagenet dataset.
vgg = VGG16(input_shape = IMAGE_SIZE + [3], weights = 'imagenet', include_top = False)  # input_shape = (64,64,3) as required by VGG

# this will exclude the initial layers from training phase as there are already been trained.
for layer in vgg.layers:
    layer.trainable = False

x = Flatten()(vgg.output)
x = Dense(512, activation = 'relu')(x)   # we can add a new fully connected layer but it will increase the execution time.
x = Dropout(0.5)(x)
x = Dense(num_classes, activation = 'softmax')(x)  # adding the output layer with softmax function as this is a multi label classification problem.

model_vgg = Model(inputs = vgg.input, outputs = x)

model_vgg.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model_vgg.summary()
Exemplo n.º 27
0
def step2():
    with open('log.txt') as fd:
        seen = set([x.strip() for x in fd.readlines()])
    with open('putToSqlQueue.txt') as fd:
        files = [fname.strip() for fname in fd.readlines()]
        files = [
            fname for fname in files
            if fname.lower().endswith('.jpg') or fname.lower().endswith('.png')
        ]
        files = [fname for fname in files if not fname in seen]
        files = [fname for fname in files if not 'MACOSX' in fname]
    print(len(files))

    from tensorflow.keras.applications.vgg16 import VGG16
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications.vgg16 import decode_predictions, preprocess_input
    from tensorflow.keras.models import Model
    from tensorflow.compiler import xla
    #from keras.applications.vgg16 import VGG16
    #from keras.preprocessing import image
    #from keras.applications.vgg16 import decode_predictions, preprocess_input
    #from keras.models import Model
    #from tensorflow.compiler import xla
    import numpy as np
    import time
    import os
    import sys
    import PIL
    import json
    import math
    import multiprocessing
    from glob import glob
    from PIL import Image
    from io import BytesIO

    model = VGG16(weights='imagenet', include_top=True)
    feat_extractor = Model(inputs=model.input,
                           outputs=model.get_layer("fc2").output)

    def prepImage(img):
        x = np.array(img.resize((224, 224)).convert('RGB'))
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        return x

    logfd = open('log.txt', 'a')

    print("Azure Blob Storage v" + __version__ + " - Python quickstart sample")

    # Create the BlobServiceClient object which will be used to create a container client
    blob_service_client = BlobServiceClient.from_connection_string(
        connectionString)
    container_client = blob_service_client.get_container_client("opengameart")

    print("\nProcessing blobs...")

    # List the blobs in the container
    for i, fname in enumerate(files):
        try:
            # Create a blob client using the local file name as the name for the blob
            print(i, "Reading blob", fname)
            starts = []
            starts.append(time.time())
            blob_client = blob_service_client.get_blob_client(
                container=container_name, blob=fname)
            imgdata = blob_client.download_blob().readall()

            print(i, "Preparing image", fname)
            starts.append(time.time())
            file_imgdata = BytesIO(imgdata)
            dt = Image.open(file_imgdata)
            pimg = prepImage(dt)

            print(i, "Computing feature vector", fname)
            starts.append(time.time())
            features = feat_extractor.predict(pimg)
            print(i, "Features", features)

            print(i, "Uploading feature vector", fname + ".np")
            starts.append(time.time())
            blob_writer = blob_service_client.get_blob_client(
                container=container_name, blob=fname + ".np")
            blob_writer.upload_blob(features.flatten().tobytes(),
                                    overwrite=True)

            end = time.time()
            print(i, ["read", "prep", "feature", "upload"],
                  [end - start for start in starts])

            print(i, "Done with", fname)

            print('')

            logfd.write(fname + '\n')

        except Exception as ex:
            print('Exception:')
            print(ex)
            time.sleep(10)

    logfd.close()
Exemplo n.º 28
0
    generator = datagen.flow_from_directory(dir_path,
                                            batch_size=10,
                                            class_mode='categorical',
                                            target_size=(150, 150))
    return generator


train_generator = load_data(
    '/content/drive/My Drive/Colab Notebooks/Mask/training')
validation_generator = load_data(
    '/content/drive/My Drive/Colab Notebooks/Mask/testing')

from tensorflow.keras.applications.vgg16 import VGG16

pretrained_model = VGG16(input_shape=(150, 150, 3),
                         weights='imagenet',
                         include_top=False)
pretrained_model.trainable = False

pretrained_model.summary()

last_layer_output = pretrained_model.get_layer('block5_pool').output
x = Flatten()(last_layer_output)
x = Dense(512, activation='relu')(x)
x = Dense(2, activation='softmax')(x)

model = Model(pretrained_model.input, x)

model.summary()

model.compile(optimizer='adam',
        MasterTensor = np.concatenate((MasterTensor, tensor), axis=0)
        MasterLabels = np.concatenate((MasterLabels, labels), axis=0)
        print('Processed class ' + str(c))
    return MasterTensor[1:, :, :, :], MasterLabels[1:, :]


# =============================================================================
# =============================================================================
""" BUILD FINE-TUNED VGG16 MODEL """

# =============================================================================
"""Convnet section"""

#Setup the convnet and add dense layers for the big tile model
conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(TileSize, TileSize, Nbands))
conv_base.summary()
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(
    layers.Dense(512,
                 activation='relu',
                 kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.5))
model.add(
    layers.Dense(256,
                 activation='relu',
                 kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dense(8, activation='softmax'))
Exemplo n.º 30
0

'''
import tensorflow as tf
from tensorflow.keras.preprocessing import image

from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from tensorflow.keras import backend as K
import numpy as np
import matplotlib.pyplot as plt
import cv2

tf.compat.v1.disable_eager_execution()

model = VGG16(
    weights='imagenet'
)  # C:\Users\BRAIN\.keras\models\vgg16_weights_tf_dim_ordering_tf_kernels.h5  540M

img_path = './creative_commons_elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))

x = image.img_to_array(img)

plt.imshow(x / 255.)

plt.show()

x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)  # list return