def build_model(self, input_dim): input1 = Input(shape=(input_dim, )) input2 = Input((config.img_x, config.img_y, 1)) input3 = Input((config.img_x, config.img_y, 1)) # 一维特征深度网络 x1 = Dense(32, activation='relu')(input1) x1 = Dropout(0.15)(x1) x1 = Dense(8, activation='relu')(x1) ## s/n 矩阵特征卷积网络 x2 = ResnetBuilder.build_resnet_18(input2, 2) #x2 = Conv2D(2, (3, 3), padding='same')(input2) # 62*62*8 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 31*31*8 #x2 = Conv2D(4, (3, 3), padding='same')(x2) # 29*29*16 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 15*15*16 #x2 = Conv2D(4, (3, 3), padding='same')(x2) # 13*13*32 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 7*7*32 #x2 = Conv2D(16, (3, 3), padding='same')(x2) # 5*5*32 #x2 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x2) # 3*3*32 #x2 = Flatten()(x2) #x2 = Dense(32, activation='relu')(x2) #x2 = Dropout(0.15)(x2) #x2 = Dense(8, activation='relu')(x2) ## s/n 矩阵特征卷积网络 x3 = ResnetBuilder.build_resnet_18(input3, 2) #x3 = Conv2D(2, (3, 3), padding='same')(input3) # 62*62*8 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 31*31*8 #x3 = Conv2D(4, (3, 3), padding='same')(x3) # 29*29*16 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 15*15*16 #x3 = Conv2D(4, (3, 3), padding='same')(x3) # 13*13*32 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 7*7*32 #x3 = Conv2D(16, (3, 3), padding='same')(x3) # 5*5*32 #x3 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x3) # 3*3*16 #x3 = Flatten()(x3) #x3 = Dense(32, activation='relu')(x3) #x3 = Dropout(0.15)(x3) #x3 = Dense(8, activation='relu')(x3) top_tensor = concatenate([x1, x2], axis=1) top_tensor = Dense(8, activation='relu')(top_tensor) top_tensor = Dropout(0.15)(top_tensor) top_tensor = Dense(2, activation='softmax')(top_tensor) #self.model = ResnetBuilder.build_resnet_18((config.img_x,config.img_y,1),2) self.model = Model(inputs=[input1, input2, input3], outputs=top_tensor) # self.model = Model(inputs=[ input1], outputs=top_tensor) sgd = keras.optimizers.SGD(lr=0.001, momentum=0.9, decay=0.00001, nesterov=False) adam = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-8) self.model.compile(loss="categorical_crossentropy", optimizer=adam, metrics=["acc"])
def _build_model(self, name, optimizer=None): from resnet import ResnetBuilder """build model from scratch""" # input: (channel, row, col) -> (1, episode_days, ticker) # output: action probability _model = ResnetBuilder.build_resnet_18(input_shape=(1, self._episode_days, self._feature_num), num_outputs=2) logger.debug('built trade model[{name}]'.format(name=name)) # ResnetBuilder.check_model(_model, name=name) if not optimizer: import keras optimizer = keras.optimizers.SGD(lr=0.01, momentum=0.9) _model.compile( optimizer=optimizer, loss={ 'policy_header': 'cosine_proximity', 'value_header': 'mean_squared_error', }, metrics={ 'policy_header': ['mse', 'acc'], 'value_header': ['mse'], }, ) logger.debug('compiled trade model[{name}]'.format(name=name)) return _model
def get_resnet_18(): from resnet import ResnetBuilder model = ResnetBuilder.build_resnet_18(input_shape=(3, 75, 75), num_outputs=2, filters=8, weight_decay=0.) return model
def my_assessor_model(mouth_nn='cnn', mouth_features_dim=512, lstm_units_1=32, dense_fc_1=128, dense_fc_2=64, conv_f_1=32, conv_f_2=64, conv_f_3=128, dropout_p=0.2): mouth_input_shape = (MOUTH_H, MOUTH_W, MOUTH_CHANNELS) my_input_mouth_images = Input(shape=(TIME_STEPS, *mouth_input_shape)) my_input_head_poses = Input(shape=(TIME_STEPS, 3)) my_input_n_of_frames = Input(shape=(1,)) my_input_lipreader_dense = Input(shape=(1024,)) my_input_lipreader_softmax = Input(shape=(500,)) if mouth_nn == 'cnn': mouth_feature_model = my_timedistributed_cnn_model((TIME_STEPS, *mouth_input_shape), conv_f_1, conv_f_2, conv_f_3, mouth_features_dim) elif mouth_nn == 'resnet18': mouth_feature_model = ResnetBuilder.build_resnet_18((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet34': mouth_feature_model = ResnetBuilder.build_resnet_34((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet50': mouth_feature_model = ResnetBuilder.build_resnet_50((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet101': mouth_feature_model = ResnetBuilder.build_resnet_101((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'resnet152': mouth_feature_model = ResnetBuilder.build_resnet_152((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True) elif mouth_nn == 'flatten': mouth_feature_model = Reshape((TIME_STEPS, -1), input_shape=(TIME_STEPS, *mouth_input_shape)) cnn_features = mouth_feature_model(my_input_mouth_images) lstm_input = Concatenate()([cnn_features, my_input_head_poses]) lstm_output = LSTM(lstm_units_1, activation='tanh', kernel_regularizer=l2(1.e-4), return_sequences=False)(lstm_input) concatenated_features = Concatenate()([lstm_output, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax]) fc1 = Dense(dense_fc_1, activation='relu', kernel_regularizer=l2(1.e-4))(concatenated_features) bn1 = BatchNormalization()(fc1) dp1 = Dropout(dropout_p)(bn1) fc2 = Dense(dense_fc_2, activation='relu', kernel_regularizer=l2(1.e-4))(dp1) bn2 = BatchNormalization()(fc2) dp2 = Dropout(dropout_p)(bn2) assessor_output = Dense(1, activation='sigmoid')(dp2) assessor = Model(inputs=[my_input_mouth_images, my_input_head_poses, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax], outputs=assessor_output) assessor.summary() return assessor
def get_model(model, ishape, nb_classes): "Return proper ResNet model" if model.endswith('18'): model = ResnetBuilder.build_resnet_18(ishape, nb_classes) elif model.endswith('34'): model = ResnetBuilder.build_resnet_34(ishape, nb_classes) elif model.endswith('50'): model = ResnetBuilder.build_resnet_50(ishape, nb_classes) elif model.endswith('101'): model = ResnetBuilder.build_resnet_101(ishape, nb_classes) elif model.endswith('152'): model = ResnetBuilder.build_resnet_152(ishape, nb_classes) else: return NotImplemented() return model
def build(self): if self.model_name == "resnet": original_resnet = self.kwargs.setdefault("original_resnet", False); if original_resnet: print("Warning: you are using original resnet"); self.model = ResnetBuilder.build_resnet_18(self.input_shape, self.classes, original_resnet=original_resnet); if self.model_name == "unet": class_mode = self.kwargs.setdefault("class_mode", "categorical"); self.model = UnetBuilder.build(self.input_shape, [64, 128, 256, 512, 1024], self.classes, class_mode=class_mode); self.model.summary(); if self.load_weights is not None: print("Loading weights: ", self.load_weights); self.model.load_weights(self.load_weights);
def build_resnet(self, model_name, lr): input_shape = (128, 128, 3) num_outputs = 100 model = None if model_name == "resnet18": model = ResnetBuilder.build_resnet_18(input_shape, num_outputs) elif model_name == "resnet34": model = ResnetBuilder.build_resnet_34(input_shape, num_outputs) elif model_name == "resnet50": model = ResnetBuilder.build_resnet_50(input_shape, num_outputs) elif model_name == "resnet50_keras": inp = Input((224, 224, 3)) model = ResNet50(input_tensor=inp, weights=None, classes=100) else: raise # opt = SGD(lr=lr, momentum=0.9, nesterov=True) opt = Adam() model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=['accuracy']) return model
def model_confirm(self, choosed_model): if choosed_model == 'AlexNet': model = alexnet(self.config) elif choosed_model == 'VGG16': model = vgg16(self.config) elif choosed_model == 'VGG19': model = vgg19(self.config) elif choosed_model == 'InceptionV3': model = inception_v3(self.config) elif choosed_model == 'InceptionV4': model = inception_v4(self.config) elif choosed_model == 'InceptionV4_ResNetV1': model = inception_resnet_v1(self.config) elif choosed_model == 'InceptionV4_ResNetV2': model = inception_resnet_v2(self.config) elif choosed_model == 'ResNet18': model = ResnetBuilder.build_resnet_18(self.config) elif choosed_model == 'ResNet34': model = ResnetBuilder.build_resnet_34(self.config) elif choosed_model == 'ResNet50': model = ResnetBuilder.build_resnet_50(self.config) elif choosed_model == 'ResNet101': model = ResnetBuilder.build_resnet_101(self.config) elif choosed_model == 'ResNet152': model = ResnetBuilder.build_resnet_152(self.config) elif choosed_model == 'DenseNet121': model = densenet121(self.config) elif choosed_model == 'DenseNet169': model = densenet169(self.config) elif choosed_model == 'DenseNet201': model = densenet201(self.config) elif choosed_model == 'DenseNet264': model = densenet264(self.config) else: model = -1 return model
def test_resnet18(): model = ResnetBuilder.build_resnet_18((3, 224, 224), 100) _test_model_compile(model)
def build_resnet_18(self, shape, num_classes): self.model = ResnetBuilder.build_resnet_18(shape, num_classes) print('[+] ResNet-18 model built') return self.model
def test_resnet18(): model = ResnetBuilder.build_resnet_18((3, 224, 224), 100) _test_model_compile(model)
with np.load(os.path.join(arguments["data_path"], "test.npz")) as data: test_imgs = data["imgs"] test_labels = data["labels"].astype(int) X_train, Y_train = np.reshape(train_imgs, (len(train_imgs), 28, 28, 1)), train_labels X_test, Y_test = np.reshape(test_imgs, (len(test_imgs), 28, 28, 1)), test_labels nb_classes = len(np.unique(np.hstack([train_labels, test_labels]))) print("Number of classes: {}".format(nb_classes), flush=True) # Create the model sess = K.get_session() graph = sess.graph # get Tensorflow graph model = ResnetBuilder.build_resnet_18((1, 28, 28), nb_classes) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(lr=arguments["learning_rate"]), metrics=['accuracy']) # Train the model # history = model.fit( # X_train, Y_train, # epochs=arguments["epochs"], # batch_size=arguments["batch_size"], # validation_data=(X_test, Y_test)) # accuracy = history.history["val_acc"][-1] print(model.outputs) import sys; sys.exit()
def ResCapsNet(input_shape, n_class, routings): # K.set_image_dim_ordering('th') x = Input(input_shape, name='input') # Part1 conv1 = Conv2D(64, (3, 3), padding='same', name='conv1')(x) bn1 = BatchNormalization(name='bn1')(conv1) relu1 = ReLU()(bn1) pool1 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool1')(relu1) drop1 = Dropout(0.3, name='dropout1')(pool1) conv2 = Conv2D(128, (3, 3), padding='same', name='conv2')(drop1) bn2 = BatchNormalization(name='bn2')(conv2) relu2 = ReLU()(bn2) pool2 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool2')(relu2) drop2 = Dropout(0.3, name='dropout2')(pool2) conv3 = Conv2D(128, (3, 3), padding='same', name='conv3')(drop2) bn3 = BatchNormalization(name='bn3')(conv3) relu3 = ReLU()(bn3) pool3 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool3')(relu3) drop3 = Dropout(0.3, name='dropout3')(pool3) conv4 = Conv2D(128, (3, 3), padding='same', name='conv4')(drop3) bn4 = BatchNormalization(name='bn4')(conv4) relu4 = ReLU()(bn4) pool4 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool4')(relu4) drop4 = Dropout(0.3, name='dropout4')(pool4) conv5 = Conv2D(128, (3, 3), padding='same', name='conv5')(drop4) bn5 = BatchNormalization(name='bn5')(conv5) relu5 = ReLU()(bn5) pool5 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool5')(relu5) drop5 = Dropout(0.3, name='dropout5')(pool5) conv6 = Conv2D(128, (3, 3), padding='same', name='conv6')(drop5) bn6 = BatchNormalization(name='bn6')(conv6) relu6 = ReLU()(bn6) pool6 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool6')(relu6) drop6 = Dropout(0.3, name='dropout6')(pool6) conv7 = Conv2D(128, (3, 3), padding='same', name='conv7')(drop6) bn7 = BatchNormalization(name='bn7')(conv7) relu7 = ReLU()(bn7) pool7 = MaxPool2D((2, 2), strides=(2, 2), padding='same', name='pool7')(relu7) drop7 = Dropout(0.3, name='dropout7')(pool7) # Part2-branch-a primarycaps = PrimaryCap(drop7, dim_capsule=8, n_channels=32, kernel_size=3, strides=2, padding='same', name='primarycaps') digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=16, routings=routings, name='digitcaps')(primarycaps) out_caps = Length(name='capsnet')(digitcaps) # fc1 = Dense(n_class, activation='sigmoid', name='fc1')(out_caps) # Part2-branch-b res = ResnetBuilder.build_resnet_18(drop7, n_class) # Part3 add = Add(name='add')([out_caps, res]) train_model = models.Model(x, add, name='ResCapsNet') return train_model
def test_resnet18(): for ordering in DIM_ORDERING: K.set_image_dim_ordering(ordering) model = ResnetBuilder.build_resnet_18((3, 224, 224), 100) model.compile(loss="categorical_crossentropy", optimizer="sgd") assert True, "Failed to build with '{}' dim ordering".format(ordering)
def Resnet18(input_shape, target_class): model = ResnetBuilder.build_resnet_18(input_shape, target_class) return model
X_train = X_train[:batch_size*(X_train.shape[0]/batch_size)] Y_train = Y_train[:batch_size*(X_train.shape[0]/batch_size)] X_test = X_test[:batch_size*(X_test.shape[0]/batch_size)] Y_test = Y_test[:batch_size*(X_test.shape[0]/batch_size)] # subtract mean and normalize mean = [125.3, 123.0, 113.9] std = [63.0, 62.1, 66.7] mean_image = np.mean(X_train, axis=0) X_train -= np.reshape(mean, [1, 1, 1, 3]) X_test -= np.reshape(mean, [1, 1, 1, 3]) X_train /= np.reshape(std, [1, 1, 1, 3]) X_test /= np.reshape(std, [1, 1, 1, 3]) # build res-18 network res_18_model = ResnetBuilder.build_resnet_18(input_shape, nb_classes, par) if par.weights: res_18_model.load_weights(par.weights, by_name=True) res_18_extractor = Model(inputs=res_18_model.input, outputs=res_18_model.get_layer('flatten_1').output) # build res-50-imagenet network vgg_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3), pooling='avg') # res_model = ResNet50(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) for l in vgg_model.layers: print l.name vgg_extractor = Model(inputs=vgg_model.input, outputs=vgg_model.get_layer('global_average_pooling2d_1').output) # res_extractor = Model(inputs=res_model.input, outputs=res_model.get_layer('avg_pool').output) model = vgg_extractor # model = res_18_extractor features = model.predict(X_test, batch_size=batch_size) print features.shape np.save('features.npy', features)
import os import keras.backend.tensorflow_backend as KTF from resnet import ResnetBuilder from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.utils import np_utils from keras import backend as K from keras.models import Sequential from keras.layers import Dense, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.utils.np_utils import to_categorical from keras.models import Model from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D from keras.layers import Input, Activation, Dense, Flatten from keras.layers.merge import add from keras.layers.normalization import BatchNormalization from keras.regularizers import l2 from keras import backend as K from keras.utils import plot_model from keras import regularizers import tensorflow as tf from keras import layers from keras.layers import Input,Add,Dense,Activation,ZeroPadding2D, \ BatchNormalization,Flatten,Conv2D,AveragePooling2D,MaxPooling2D,GlobalMaxPooling2D from resnet import ResnetBuilder import keras.backend as K model = ResnetBuilder.build_resnet_18((1, 224, 224), 20) from keras.utils import plot_model plot_model(model, to_file='model.png', show_shapes=1)
import pytest from keras import backend as K from keras.preprocessing import image from resnet import ResnetBuilder import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' model = ResnetBuilder.build_resnet_18((3, 224, 224), 100) K.set_image_dim_ordering('tf') model.compile(loss="categorical_crossentropy", optimizer="sgd") # _test_model_compile(model) # load an image img_dir = 'images' img_name = 'stanford.jpg' img_path = os.path.join(img_dir, img_name) img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) np.shape(x) y_hat = model.predict(x) np.shape(y_hat) # model.summary() import pandas as pd pd.read_csv('resnet18_cifar10.csv')
"Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot", ] input_shape = (28, 28, 1) # モデルの入力サイズ num_classes = 10 # クラス数 # ResNet18 モデルを作成する。 model = ResnetBuilder.build_resnet_18(input_shape, num_classes) # モデルをコンパイルする。 model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) # 学習する。 model.fit(train_X, train_labels, epochs=5) # テストデータに対する性能を確認する。 test_loss, test_acc = model.evaluate(test_X, test_labels) print(f"test loss: {test_loss:.2f}, test accuracy: {test_acc:.2%}") # test loss: 0.36, test accuracy: 86.93%