def create(self, size, include_top): if size != (224,224): include_top=False model = self.model = Sequential() model.add(Lambda(vgg_preprocess, input_shape=(3,)+size, output_shape=(3,)+size)) self.ConvBlock(2, 64) self.ConvBlock(2, 128) self.ConvBlock(3, 256) self.ConvBlock(3, 512) self.ConvBlock(3, 512) if not include_top: fname = 'vgg16_bn_conv.h5' model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models')) return model.add(Flatten()) self.FCBlock() self.FCBlock() model.add(Dense(1000, activation='softmax')) fname = 'vgg16_bn.h5' model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models'))
def load_vgg_weight(self, model): # Loading VGG 16 weights if K.image_dim_ordering() == "th": weights = get_file('vgg16_weights_th_dim_ordering_th_kernels_notop.h5', THEANO_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: weights = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models') f = h5py.File(weights) layer_names = [name for name in f.attrs['layer_names']] if self.vgg_layers is None: self.vgg_layers = [layer for layer in model.layers if 'vgg_' in layer.name] for i, layer in enumerate(self.vgg_layers): g = f[layer_names[i]] weights = [g[name] for name in g.attrs['weight_names']] layer.set_weights(weights) # Freeze all VGG layers for layer in self.vgg_layers: layer.trainable = False return model
def decode_predictions(preds, top=5): LABELS = None if len(preds.shape) == 2: if preds.shape[1] == 2622: fpath = get_file('rcmalli_vggface_labels_v1.npy', V1_LABELS_PATH, cache_subdir=VGGFACE_DIR) LABELS = np.load(fpath) elif preds.shape[1] == 8631: fpath = get_file('rcmalli_vggface_labels_v2.npy', V2_LABELS_PATH, cache_subdir=VGGFACE_DIR) LABELS = np.load(fpath) else: raise ValueError('`decode_predictions` expects ' 'a batch of predictions ' '(i.e. a 2D array of shape (samples, 2622)) for V1 or ' '(samples, 8631) for V2.' 'Found array with shape: ' + str(preds.shape)) else: raise ValueError('`decode_predictions` expects ' 'a batch of predictions ' '(i.e. a 2D array of shape (samples, 2622)) for V1 or ' '(samples, 8631) for V2.' 'Found array with shape: ' + str(preds.shape)) results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] result = [[str(LABELS[i].encode('utf8')), pred[i]] for i in top_indices] result.sort(key=lambda x: x[1], reverse=True) results.append(result) return results
def download_from_cloud(model_file_name, json_url, h5_url): print('Downloading from cloud') json_file_name, h5_file_name = SequenceModel.get_full_file_names(model_file_name) downloaded_json = get_file(os.path.normpath(json_file_name), origin=json_url) if downloaded_json != json_file_name: shutil.copy(downloaded_json, json_file_name) downloaded_h5 = get_file(os.path.normpath(h5_file_name), origin=h5_url) if downloaded_h5 != h5_file_name: shutil.copy(downloaded_h5, h5_file_name)
def load_data(): """ Load CIFAR10 dataset. # Returns Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. """ dirname = 'cifar-10-batches-py' origin = 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz' path = get_file(dirname, origin=origin, untar=True) num_train_samples = 50000 x_train = np.zeros((num_train_samples, 3, 32, 32), dtype='uint8') y_train = np.zeros((num_train_samples,), dtype='uint8') for i in range(1, 6): fpath = os.path.join(path, 'data_batch_' + str(i)) data, labels = load_batch(fpath) x_train[(i - 1) * 10000: i * 10000, :, :, :] = data y_train[(i - 1) * 10000: i * 10000] = labels fpath = os.path.join(path, 'test_batch') x_test, y_test = load_batch(fpath) y_train = np.reshape(y_train, (len(y_train), 1)) y_test = np.reshape(y_test, (len(y_test), 1)) if K.image_data_format() == 'channels_last': x_train = x_train.transpose(0, 2, 3, 1) x_test = x_test.transpose(0, 2, 3, 1) return {'x_train': x_train, 'y_train': y_train, 'x_test': x_test, 'y_test': y_test}
def load_data(config): """ Load STL10 dataset. Returns ------- dict """ # url of the binary data origin = 'http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gz' dirname = 'stl10_binary' path = get_file(dirname, origin=origin, untar=True) # Load training data data_path = os.path.join(path, 'train_X.bin') label_path = os.path.join(path, 'train_y.bin') x_train = read_all_images(data_path) y_train = read_labels(label_path) # Load test data data_path = os.path.join(path, 'test_X.bin') label_path = os.path.join(path, 'test_y.bin') x_test = read_all_images(data_path) y_test = read_labels(label_path) x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.10, random_state=42, stratify=y_train) return {'x_train': x_train, 'y_train': y_train, 'x_val': x_val, 'y_val': y_val, 'x_test': x_test, 'y_test': y_test}
def decode_predictions(preds, top=5): """ Decode the predictionso of the ImageNet trained network. Parameters ---------- preds : numpy array top : int How many predictions to return Returns ------- list of tuples e.g. (u'n02206856', u'bee', 0.71072823) for the WordNet identifier, the class name and the probability. """ global CLASS_INDEX if len(preds.shape) != 2 or preds.shape[1] != 1000: raise ValueError('`decode_predictions` expects ' 'a batch of predictions ' '(i.e. a 2D array of shape (samples, 1000)). ' 'Found array with shape: ' + str(preds.shape)) if CLASS_INDEX is None: fpath = get_file('imagenet_class_index.json', CLASS_INDEX_PATH, cache_subdir='models') CLASS_INDEX = json.load(open(fpath)) results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] results.append(result) return results
def load_data(): """Loads the Fashion-MNIST dataset. # Returns Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. """ dirname = os.path.join('datasets', 'fashion-mnist') base = 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' files = ['train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'] paths = [] for file in files: paths.append(get_file(file, origin=base + file, cache_subdir=dirname)) with gzip.open(paths[0], 'rb') as lbpath: y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8) with gzip.open(paths[1], 'rb') as imgpath: x_train = np.frombuffer(imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28) with gzip.open(paths[2], 'rb') as lbpath: y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8) with gzip.open(paths[3], 'rb') as imgpath: x_test = np.frombuffer(imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28) return (x_train, y_train), (x_test, y_test)
def load_data(config): """ Load the MNIST dataset. # Arguments path: path where to cache the dataset locally (relative to ~/.keras/datasets). # Returns Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. """ path = 'mnist.npz' path = get_file(path, origin='https://s3.amazonaws.com/img-datasets/mnist.npz') f = np.load(path) x_train = f['x_train'] y_train = f['y_train'] x_test = f['x_test'] y_test = f['y_test'] f.close() x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.10, random_state=42, stratify=y_train) return {'x_train': x_train, 'y_train': y_train, 'x_val': x_val, 'y_val': y_val, 'x_test': x_test, 'y_test': y_test}
def load_data(label_mode='fine'): """Loads CIFAR100 dataset. # Arguments label_mode: one of "fine", "coarse". # Returns Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. # Raises ValueError: in case of invalid `label_mode`. """ if label_mode not in ['fine', 'coarse']: raise ValueError('label_mode must be one of "fine" "coarse".') dirname = 'cifar-100-python' origin = 'http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz' path = get_file(dirname, origin=origin, untar=True) fpath = os.path.join(path, 'train') x_train, y_train = load_batch(fpath, label_key=label_mode + '_labels') fpath = os.path.join(path, 'test') x_test, y_test = load_batch(fpath, label_key=label_mode + '_labels') y_train = np.reshape(y_train, (len(y_train), 1)) y_test = np.reshape(y_test, (len(y_test), 1)) if K.image_data_format() == 'channels_last': x_train = x_train.transpose(0, 2, 3, 1) x_test = x_test.transpose(0, 2, 3, 1) return {'x_train': x_train, 'y_train': y_train, 'x_test': x_test, 'y_test': y_test}
def create(self): """ Creates the VGG16 network achitecture and loads the pretrained weights. Args: None Returns: None """ model = self.model = Sequential() model.add(Lambda(vgg_preprocess, input_shape=(3,224,224), output_shape=(3,224,224))) self.ConvBlock(2, 64) self.ConvBlock(2, 128) self.ConvBlock(3, 256) self.ConvBlock(3, 512) self.ConvBlock(3, 512) model.add(Flatten()) self.FCBlock() self.FCBlock() model.add(Dense(1000, activation='softmax')) fname = 'vgg16.h5' model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models')) from keras.utils.conv_utils import convert_kernel import tensorflow as tf ops = [] for layer in model.layers: if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D', 'Conv1D', 'Conv2D', 'Conv3D']: original_w, original_b = layer.get_weights() converted_w = convert_kernel(original_w) layer.set_weights([converted_w, original_b])
def get_classes(self): """ Downloads the Imagenet classes index file and loads it to self.classes. The file is downloaded only if it not already in the cache. """ fname = 'imagenet_class_index.json' fpath = get_file(fname, self.FILE_PATH+fname, cache_subdir='models') with open(fpath) as f: class_dict = json.load(f) self.classes = [class_dict[str(i)][1] for i in range(len(class_dict))]
def ZF_UNET_224(dropout_val=0.2, weights=None): if K.image_dim_ordering() == 'th': inputs = Input((INPUT_CHANNELS, 224, 224)) axis = 1 else: inputs = Input((224, 224, INPUT_CHANNELS)) axis = 3 filters = 32 conv_224 = double_conv_layer(inputs, filters) pool_112 = MaxPooling2D(pool_size=(2, 2))(conv_224) conv_112 = double_conv_layer(pool_112, 2*filters) pool_56 = MaxPooling2D(pool_size=(2, 2))(conv_112) conv_56 = double_conv_layer(pool_56, 4*filters) pool_28 = MaxPooling2D(pool_size=(2, 2))(conv_56) conv_28 = double_conv_layer(pool_28, 8*filters) pool_14 = MaxPooling2D(pool_size=(2, 2))(conv_28) conv_14 = double_conv_layer(pool_14, 16*filters) pool_7 = MaxPooling2D(pool_size=(2, 2))(conv_14) conv_7 = double_conv_layer(pool_7, 32*filters) up_14 = concatenate([UpSampling2D(size=(2, 2))(conv_7), conv_14], axis=axis) up_conv_14 = double_conv_layer(up_14, 16*filters) up_28 = concatenate([UpSampling2D(size=(2, 2))(up_conv_14), conv_28], axis=axis) up_conv_28 = double_conv_layer(up_28, 8*filters) up_56 = concatenate([UpSampling2D(size=(2, 2))(up_conv_28), conv_56], axis=axis) up_conv_56 = double_conv_layer(up_56, 4*filters) up_112 = concatenate([UpSampling2D(size=(2, 2))(up_conv_56), conv_112], axis=axis) up_conv_112 = double_conv_layer(up_112, 2*filters) up_224 = concatenate([UpSampling2D(size=(2, 2))(up_conv_112), conv_224], axis=axis) up_conv_224 = double_conv_layer(up_224, filters, dropout_val) conv_final = Conv2D(OUTPUT_MASK_CHANNELS, (1, 1))(up_conv_224) conv_final = Activation('sigmoid')(conv_final) model = Model(inputs, conv_final, name="ZF_UNET_224") if weights == 'generator' and axis == 3 and INPUT_CHANNELS == 3 and OUTPUT_MASK_CHANNELS == 1: weights_path = get_file( 'zf_unet_224_weights_tf_dim_ordering_tf_generator.h5', ZF_UNET_224_WEIGHT_PATH, cache_subdir='models', file_hash='203146f209baf34ac0d793e1691f1ab7') model.load_weights(weights_path) return model
def main(): args = get_args() depth = args.depth k = args.width weight_file = args.weight_file if not weight_file: weight_file = get_file("weights.18-4.06.hdf5", pretrained_model, cache_subdir="pretrained_models", file_hash=modhash, cache_dir=os.path.dirname(os.path.abspath(__file__))) # for face detection detector = dlib.get_frontal_face_detector() # load model and weights img_size = 64 model = WideResNet(img_size, depth=depth, k=k)() model.load_weights(weight_file) for img in yield_images(): input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_h, img_w, _ = np.shape(input_img) # detect faces using dlib detector detected = detector(input_img, 1) faces = np.empty((len(detected), img_size, img_size, 3)) if len(detected) > 0: for i, d in enumerate(detected): x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height() xw1 = max(int(x1 - 0.4 * w), 0) yw1 = max(int(y1 - 0.4 * h), 0) xw2 = min(int(x2 + 0.4 * w), img_w - 1) yw2 = min(int(y2 + 0.4 * h), img_h - 1) cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2) # cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2) faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size)) # predict ages and genders of the detected faces results = model.predict(faces) predicted_genders = results[0] ages = np.arange(0, 101).reshape(101, 1) predicted_ages = results[1].dot(ages).flatten() # draw results for i, d in enumerate(detected): label = "{}, {}".format(int(predicted_ages[i]), "F" if predicted_genders[i][0] > 0.5 else "M") draw_label(img, (d.left(), d.top()), label) cv2.imshow("result", img) key = cv2.waitKey(30) if key == 27: break
def test_data_utils(in_tmpdir): """Tests get_file from a url, plus extraction and validation. """ dirname = 'data_utils' with open('test.txt', 'w') as text_file: text_file.write('Float like a butterfly, sting like a bee.') with tarfile.open('test.tar.gz', 'w:gz') as tar_file: tar_file.add('test.txt') with zipfile.ZipFile('test.zip', 'w') as zip_file: zip_file.write('test.txt') origin = urljoin('file://', pathname2url(os.path.abspath('test.tar.gz'))) path = get_file(dirname, origin, untar=True) filepath = path + '.tar.gz' hashval_sha256 = _hash_file(filepath) hashval_md5 = _hash_file(filepath, algorithm='md5') path = get_file(dirname, origin, md5_hash=hashval_md5, untar=True) path = get_file(filepath, origin, file_hash=hashval_sha256, extract=True) assert os.path.exists(filepath) assert validate_file(filepath, hashval_sha256) assert validate_file(filepath, hashval_md5) os.remove(filepath) os.remove('test.tar.gz') origin = urljoin('file://', pathname2url(os.path.abspath('test.zip'))) hashval_sha256 = _hash_file('test.zip') hashval_md5 = _hash_file('test.zip', algorithm='md5') path = get_file(dirname, origin, md5_hash=hashval_md5, extract=True) path = get_file(dirname, origin, file_hash=hashval_sha256, extract=True) assert os.path.exists(path) assert validate_file(path, hashval_sha256) assert validate_file(path, hashval_md5) os.remove(path) os.remove('test.txt') os.remove('test.zip')
def decode_predictions(preds): global CLASS_INDEX assert len(preds.shape) == 2 and preds.shape[1] == 1000 if CLASS_INDEX is None: fpath = get_file('imagenet_class_index.json', CLASS_INDEX_PATH, cache_subdir='models') CLASS_INDEX = json.load(open(fpath)) indices = np.argmax(preds, axis=-1) results = [] for i in indices: results.append(CLASS_INDEX[str(i)]) return results
def load_data(data_file, url): """loads the data from the gzip pickled files, and converts to numpy arrays""" print('loading data ...') path = get_file(data_file, origin=url) f = gzip.open(path, 'rb') train_set, valid_set, test_set = load_pickle(f) f.close() train_set_x, train_set_y = make_numpy_array(train_set) valid_set_x, valid_set_y = make_numpy_array(valid_set) test_set_x, test_set_y = make_numpy_array(test_set) return [(train_set_x, train_set_y), (valid_set_x, valid_set_y), (test_set_x, test_set_y)]
def load_data(): ''' load data from MovieLens 100K Dataset http://grouplens.org/datasets/movielens/ Note that this method uses ua.base and ua.test in the dataset. :return: train_users, train_x, test_users, test_x :rtype: list of int, numpy.array, list of int, numpy.array ''' path = get_file('ml-100k.zip', origin='http://files.grouplens.org/datasets/movielens/ml-100k.zip') with ZipFile(path, 'r') as ml_zip: max_item_id = -1 train_history = {} with ml_zip.open('ml-100k/ua.base', 'r') as file: for line in file: user_id, item_id, rating, timestamp = line.decode('utf-8').rstrip().split('\t') if int(user_id) not in train_history: train_history[int(user_id)] = [int(item_id)] else: train_history[int(user_id)].append(int(item_id)) if max_item_id < int(item_id): max_item_id = int(item_id) test_history = {} with ml_zip.open('ml-100k/ua.test', 'r') as file: for line in file: user_id, item_id, rating, timestamp = line.decode('utf-8').rstrip().split('\t') if int(user_id) not in test_history: test_history[int(user_id)] = [int(item_id)] else: test_history[int(user_id)].append(int(item_id)) max_item_id += 1 # item_id starts from 1 train_users = list(train_history.keys()) train_x = numpy.zeros((len(train_users), max_item_id), dtype=numpy.int32) for i, hist in enumerate(train_history.values()): mat = to_categorical(hist, max_item_id) train_x[i] = numpy.sum(mat, axis=0) test_users = list(test_history.keys()) test_x = numpy.zeros((len(test_users), max_item_id), dtype=numpy.int32) for i, hist in enumerate(test_history.values()): mat = to_categorical(hist, max_item_id) test_x[i] = numpy.sum(mat, axis=0) return train_users, train_x, test_users, test_x
def main(): args = get_args() depth = args.depth k = args.width weight_file = args.weight_file if not weight_file: weight_file = get_file("weights.18-4.06.hdf5", pretrained_model, cache_subdir="pretrained_models", file_hash=modhash, cache_dir=os.path.dirname(os.path.abspath(__file__))) # load model and weights img_size = 64 batch_size = 32 model = WideResNet(img_size, depth=depth, k=k)() model.load_weights(weight_file) dataset_root = Path(__file__).parent.joinpath("appa-real", "appa-real-release") validation_image_dir = dataset_root.joinpath("valid") gt_valid_path = dataset_root.joinpath("gt_avg_valid.csv") image_paths = list(validation_image_dir.glob("*_face.jpg")) faces = np.empty((batch_size, img_size, img_size, 3)) ages = [] image_names = [] for i, image_path in tqdm(enumerate(image_paths)): faces[i % batch_size] = cv2.resize(cv2.imread(str(image_path), 1), (img_size, img_size)) image_names.append(image_path.name[:-9]) if (i + 1) % batch_size == 0 or i == len(image_paths) - 1: results = model.predict(faces) ages_out = np.arange(0, 101).reshape(101, 1) predicted_ages = results[1].dot(ages_out).flatten() ages += list(predicted_ages) # len(ages) can be larger than len(image_names) due to the last batch, but it's ok. name2age = {image_names[i]: ages[i] for i in range(len(image_names))} df = pd.read_csv(str(gt_valid_path)) appa_abs_error = 0.0 real_abs_error = 0.0 for i, row in df.iterrows(): appa_abs_error += abs(name2age[row.file_name] - row.apparent_age_avg) real_abs_error += abs(name2age[row.file_name] - row.real_age) print("MAE Apparent: {}".format(appa_abs_error / len(image_names))) print("MAE Real: {}".format(real_abs_error / len(image_names)))
def create(self): model = self.model = Sequential() model.add(Lambda(vgg_preprocess, input_shape=(3,224,224), output_shape=(3,224,224))) self.ConvBlock(2, 64) self.ConvBlock(2, 128) self.ConvBlock(3, 256) self.ConvBlock(3, 512) self.ConvBlock(3, 512) model.add(Flatten()) self.FCBlock() self.FCBlock() model.add(Dense(1000, activation='softmax')) fname = 'vgg16.h5' model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models'))
def load_data(config): """ Load CIFAR100 dataset. Parameters ---------- label_mode: one of "fine", "coarse". Returns ------- Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. Raises ------ ValueError: in case of invalid `label_mode`. """ label_mode = 'fine' if label_mode not in ['fine', 'coarse']: raise ValueError('label_mode must be one of "fine" "coarse".') dirname = 'cifar-100-python' origin = 'http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz' path = get_file(dirname, origin=origin, untar=True) fpath = os.path.join(path, 'train') x_train, y_train = load_batch(fpath, label_key=label_mode + '_labels') fpath = os.path.join(path, 'test') x_test, y_test = load_batch(fpath, label_key=label_mode + '_labels') y_train = np.reshape(y_train, (len(y_train), 1)) y_test = np.reshape(y_test, (len(y_test), 1)) if K.image_data_format() == 'channels_last': x_train = x_train.transpose(0, 2, 3, 1) x_test = x_test.transpose(0, 2, 3, 1) x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.10, random_state=42, stratify=y_train) return {'x_train': x_train, 'y_train': y_train, 'x_val': x_val, 'y_val': y_val, 'x_test': x_test, 'y_test': y_test}
def __init__(self, fname, origin, inlen=100, outlen=50, step=10, strip_ws=True): self.inlen = inlen self.outlen = outlen self.step = step self.path = get_file(fname, origin=origin) text = open(self.path, encoding="utf-8").read().lower() if strip_ws: text = re.sub(' +', ' ', text).strip() text = re.sub('\n+', '\n', text).strip() self.chars = sorted(list(set(text))) self.char_indices = dict((c, i) for i, c in enumerate(self.chars)) self.indices_char = dict((i, c) for i, c in enumerate(self.chars)) self.build_dataset(text)
def decode_predictions(preds, top=5): global CLASS_INDEX if len(preds.shape) != 2 or preds.shape[1] != 1000: raise ValueError('`decode_predictions` expects ' 'a batch of predictions ' '(i.e. a 2D array of shape (samples, 1000)). ' 'Found array with shape: ' + str(preds.shape)) if CLASS_INDEX is None: fpath = get_file('imagenet_class_index.json', CLASS_INDEX_PATH, cache_subdir='models') CLASS_INDEX = json.load(open(fpath)) results = [] for pred in preds: top_indices = pred.argsort()[-top:][::-1] result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] results.append(result) return results
def train(run_name, start_epoch, stop_epoch, img_w): # Input Parameters img_h = 64 words_per_epoch = 16000 val_split = 0.2 val_words = int(words_per_epoch * (val_split)) pool_size = 2 if K.image_data_format() == 'channels_first': input_shape = (1, img_w, img_h) else: input_shape = (img_w, img_h, 1) fdir = os.path.dirname(get_file('wordlists.tgz', origin='http://www.mythic-ai.com/datasets/wordlists.tgz', untar=True)) img_gen = TextImageGenerator(monogram_file=os.path.join(fdir, 'wordlist_mono_clean.txt'), bigram_file=os.path.join(fdir, 'wordlist_bi_clean.txt'), minibatch_size=32, img_w=img_w, img_h=img_h, downsample_factor=(pool_size ** 2), val_split=words_per_epoch - val_words ) # clipnorm seems to speeds up convergence sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5) model, input_data, y_pred, test_func = create_model(input_shape, img_gen, pool_size, img_w, img_h) # the loss calc occurs elsewhere, so use a dummy lambda func for the loss model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd) if start_epoch > 0: weight_file = os.path.join(OUTPUT_DIR, os.path.join(run_name, 'weights%02d.h5' % (start_epoch - 1))) model.load_weights(weight_file) viz_cb = VizCallback(run_name, test_func, img_gen.next_val()) model.fit_generator(generator=img_gen.next_train(), steps_per_epoch=(words_per_epoch - val_words), epochs=stop_epoch, validation_data=img_gen.next_val(), validation_steps=val_words, callbacks=[viz_cb, img_gen], initial_epoch=start_epoch) return model
def __init__(self, inputs, blocks, weights=None, trainable=True, name='encoder'): inverse_pyramid = [] # convolutional block conv_blocks = blocks[:-1] for i, block in enumerate(conv_blocks): if i == 0: x = block(inputs) inverse_pyramid.append(x) elif i < len(conv_blocks) - 1: x = block(x) inverse_pyramid.append(x) else: x = block(x) # fully convolutional block fc_block = blocks[-1] y = fc_block(x) inverse_pyramid.append(y) outputs = list(reversed(inverse_pyramid)) super(Encoder, self).__init__( inputs=inputs, outputs=outputs) # load pre-trained weights if weights is not None: weights_path = get_file( '{}_weights_tf_dim_ordering_tf_kernels.h5'.format(name), weights, cache_subdir='models') layer_names = load_weights(self, weights_path) if K.image_data_format() == 'channels_first': layer_utils.convert_all_kernels_in_model(self) # Freezing basenet weights if trainable is False: for layer in self.layers: if layer.name in layer_names: layer.trainable = False
def __init__(self, frame_size=40, hop_size=3): self.frame_size = frame_size self.hop_size = hop_size path = get_file('nietzsche.txt', origin="https://s3.amazonaws.com/text-datasets/nietzsche.txt") self.text = open(path).read().lower() print('corpus length:', len(self.text)) chars = sorted(list(set(self.text))) self.class_count = len(chars) print('total chars:', self.class_count) self.le = LabelEncoder().fit(chars) self.text_ohe = self.text_to_ohe(self.text) def split_to_frames(values, frame_size, hop_size): """ Split to overlapping frames. """ return np.stack(values[i:i + frame_size] for i in range(0, len(values) - frame_size + 1, hop_size)) def split_features_targets(frames): """ Split each frame to features (all but last element) and targets (last element). """ frame_size = frames.shape[1] X = frames[:, :frame_size - 1] y = frames[:, -1] return X, y # cut the text in semi-redundant sequences of frame_size characters self.X, self.y = split_features_targets(split_to_frames( self.text_ohe, frame_size + 1, hop_size)) print('X.shape:', self.X.shape, 'y.shape:', self.y.shape)
def load_data(path='mnist.npz'): """Loads the MNIST dataset. # Arguments path: path where to cache the dataset locally (relative to ~/.keras/datasets). # Returns Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. """ path = get_file(path, origin='https://s3.amazonaws.com/img-datasets/mnist.npz') f = np.load(path) x_train = f['x_train'] x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) y_train = f['y_train'] x_test = f['x_test'] x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) y_test = f['y_test'] f.close() return {'x_train': x_train, 'y_train': y_train, 'x_test': x_test, 'y_test': y_test}
def create(self, size, include_top): input_shape = (3,)+size img_input = Input(shape=input_shape) bn_axis = 1 x = Lambda(self.vgg_preprocess)(img_input) x = ZeroPadding2D((3, 3))(x) x = Convolution2D(64, 7, 7, subsample=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') for n in ['b','c','d']: x = identity_block(x, 3, [128, 128, 512], stage=3, block=n) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') for n in ['b','c','d', 'e', 'f']: x = identity_block(x, 3, [256, 256, 1024], stage=4, block=n) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') if include_top: x = AveragePooling2D((7, 7), name='avg_pool')(x) x = Flatten()(x) x = Dense(1000, activation='softmax', name='fc1000')(x) fname = 'resnet50.h5' else: fname = 'resnet_nt.h5' self.img_input = img_input self.model = Model(self.img_input, x) convert_all_kernels_in_model(self.model) self.model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models'))
y[word_idx[answer]] = 1 X.append(x) Xq.append(xq) Y.append(y) return pad_sequences(X, maxlen=story_maxlen), pad_sequences(Xq, maxlen=query_maxlen), np.array(Y) RNN = recurrent.LSTM EMBED_HIDDEN_SIZE = 50 SENT_HIDDEN_SIZE = 100 QUERY_HIDDEN_SIZE = 100 BATCH_SIZE = 32 EPOCHS = 40 print('RNN / Embed / Sent / Query = {}, {}, {}, {}'.format(RNN, EMBED_HIDDEN_SIZE, SENT_HIDDEN_SIZE, QUERY_HIDDEN_SIZE)) try: path = get_file('babi-tasks-v1-2.tar.gz', origin='https://s3.amazonaws.com/text-datasets/babi_tasks_1-20_v1-2.tar.gz') except: print('Error downloading dataset, please download it manually:\n' '$ wget http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz\n' '$ mv tasks_1-20_v1-2.tar.gz ~/.keras/datasets/babi-tasks-v1-2.tar.gz') raise tar = tarfile.open(path) # Default QA1 with 1000 samples # challenge = 'tasks_1-20_v1-2/en/qa1_single-supporting-fact_{}.txt' # QA1 with 10,000 samples # challenge = 'tasks_1-20_v1-2/en-10k/qa1_single-supporting-fact_{}.txt' # QA2 with 1000 samples challenge = 'tasks_1-20_v1-2/en/qa2_two-supporting-facts_{}.txt' # QA2 with 10,000 samples # challenge = 'tasks_1-20_v1-2/en-10k/qa2_two-supporting-facts_{}.txt' train = get_stories(tar.extractfile(challenge.format('train')))
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, dropout_keep_prob=0.8): """Instantiates the Inception-ResNet v2 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `"image_data_format": "channels_last"` in your Keras config at `~/.keras/keras.json`. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299, instead of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing function is different (i.e., do not use `imagenet_utils.preprocess_input()` with this model. Use `preprocess_input()` defined in this module instead). # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or `'imagenet'` (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is `False` (otherwise the input shape has to be `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `'avg'` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `'max'` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is `True`, and if no `weights` argument is specified. dropout_keep_prob: dropout keep rate after pooling and before the classification layer, only to be specified if `include_top` is `True`. # Returns A Keras `Model` instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=299, min_size=139, data_format=K.image_data_format(), require_flatten=False, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Stem block: 35 x 35 x 192 x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid', name='Conv2d_1a_3x3') x = conv2d_bn(x, 32, 3, padding='valid', name='Conv2d_2a_3x3') x = conv2d_bn(x, 64, 3, name='Conv2d_2b_3x3') x = MaxPooling2D(3, strides=2, name='MaxPool_3a_3x3')(x) x = conv2d_bn(x, 80, 1, padding='valid', name='Conv2d_3b_1x1') x = conv2d_bn(x, 192, 3, padding='valid', name='Conv2d_4a_3x3') x = MaxPooling2D(3, strides=2, name='MaxPool_5a_3x3')(x) # Mixed 5b (Inception-A block): 35 x 35 x 320 channel_axis = 1 if K.image_data_format() == 'channels_first' else 3 name_fmt = partial(_generate_layer_name, prefix='Mixed_5b') branch_0 = conv2d_bn(x, 96, 1, name=name_fmt('Conv2d_1x1', 0)) branch_1 = conv2d_bn(x, 48, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 64, 5, name=name_fmt('Conv2d_0b_5x5', 1)) branch_2 = conv2d_bn(x, 64, 1, name=name_fmt('Conv2d_0a_1x1', 2)) branch_2 = conv2d_bn(branch_2, 96, 3, name=name_fmt('Conv2d_0b_3x3', 2)) branch_2 = conv2d_bn(branch_2, 96, 3, name=name_fmt('Conv2d_0c_3x3', 2)) branch_pool = AveragePooling2D(3, strides=1, padding='same', name=name_fmt('AvgPool_0a_3x3', 3))(x) branch_pool = conv2d_bn(branch_pool, 64, 1, name=name_fmt('Conv2d_0b_1x1', 3)) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_5b')(branches) # 10x Block35 (Inception-ResNet-A block): 35 x 35 x 320 for block_idx in range(1, 11): x = _inception_resnet_block(x, scale=0.17, block_type='Block35', block_idx=block_idx) # Mixed 6a (Reduction-A block): 17 x 17 x 1088 name_fmt = partial(_generate_layer_name, prefix='Mixed_6a') branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 0)) branch_1 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 256, 3, name=name_fmt('Conv2d_0b_3x3', 1)) branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 1)) branch_pool = MaxPooling2D(3, strides=2, padding='valid', name=name_fmt('MaxPool_1a_3x3', 2))(x) branches = [branch_0, branch_1, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_6a')(branches) # 20x Block17 (Inception-ResNet-B block): 17 x 17 x 1088 for block_idx in range(1, 21): x = _inception_resnet_block(x, scale=0.1, block_type='Block17', block_idx=block_idx) # Mixed 7a (Reduction-B block): 8 x 8 x 2080 name_fmt = partial(_generate_layer_name, prefix='Mixed_7a') branch_0 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 0)) branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 0)) branch_1 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 1)) branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 1)) branch_2 = conv2d_bn(x, 256, 1, name=name_fmt('Conv2d_0a_1x1', 2)) branch_2 = conv2d_bn(branch_2, 288, 3, name=name_fmt('Conv2d_0b_3x3', 2)) branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid', name=name_fmt('Conv2d_1a_3x3', 2)) branch_pool = MaxPooling2D(3, strides=2, padding='valid', name=name_fmt('MaxPool_1a_3x3', 3))(x) branches = [branch_0, branch_1, branch_2, branch_pool] x = Concatenate(axis=channel_axis, name='Mixed_7a')(branches) # 10x Block8 (Inception-ResNet-C block): 8 x 8 x 2080 for block_idx in range(1, 10): x = _inception_resnet_block(x, scale=0.2, block_type='Block8', block_idx=block_idx) x = _inception_resnet_block(x, scale=1., activation=None, block_type='Block8', block_idx=10) # Final convolution block x = conv2d_bn(x, 1536, 1, name='Conv2d_7b_1x1') if include_top: # Classification block x = GlobalAveragePooling2D(name='AvgPool')(x) x = Dropout(1.0 - dropout_keep_prob, name='Dropout')(x) x = Dense(classes, name='Logits')(x) x = Activation('softmax', name='Predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D(name='AvgPool')(x) elif pooling == 'max': x = GlobalMaxPooling2D(name='MaxPool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor` if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model model = Model(inputs, x, name='inception_resnet_v2') # Load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', md5_hash='e693bd0210a403b3192acc6073ad2e96') else: weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5' weights_path = get_file( weights_filename, BASE_WEIGHT_URL + weights_filename, cache_subdir='models', md5_hash='d19885ff4a710c122648d3b5c3b684e4') model.load_weights(weights_path) return model
question1 = [] with open(KERAS_DATASETS_DIR + QUESTION_PAIRS_FILE) as csvfile: reader = csv.DictReader(csvfile, delimiter='\t') for row in reader: question1.append(row['question1']) TRAIN_LENGTH = len(question1) q1_data = np.load(open(Q1_TRAINING_DATA_FILE, 'rb')) q2_data = np.load(open(Q2_TRAINING_DATA_FILE, 'rb')) labels = np.load(open(LABEL_TRAINING_DATA_FILE, 'rb')) word_embedding_matrix = np.load(open(WORD_EMBEDDING_MATRIX_FILE, 'rb')) with open(NB_WORDS_DATA_FILE, 'r') as f: nb_words = json.load(f)['nb_words'] else: if not exists(KERAS_DATASETS_DIR + QUESTION_PAIRS_FILE): get_file(QUESTION_PAIRS_FILE, QUESTION_PAIRS_FILE_URL) print("Processing", QUESTION_PAIRS_FILE) question1 = [] question2 = [] is_duplicate = [] with open(KERAS_DATASETS_DIR + QUESTION_PAIRS_FILE) as csvfile: reader = csv.DictReader(csvfile, delimiter='\t') for row in reader: question1.append(row['question1']) question2.append(row['question2']) is_duplicate.append(row['is_duplicate']) print('Question pairs: %d' % len(question1)) TRAIN_LENGTH = len(question1)
def get_classes(self): fname = 'imagenet_class_index.json' fpath = get_file(fname, self.FILE_PATH + fname, cache_subdir='models') with open(fpath) as f: class_dict = json.load(f) self.classes = [class_dict[str(i)][1] for i in range(len(class_dict))]
def create_inception_v4(nb_classes=1001, load_weights=True): ''' Creates a inception v4 network :param nb_classes: number of classes.txt :return: Keras Model with 1 input and 1 output ''' if K.image_data_format() == 'th': init = Input((3, 299, 299)) else: init = Input((299, 299, 3)) # Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th) x = inception_stem(init) # 4 x Inception A for i in range(4): x = inception_A(x) # Reduction A x = reduction_A(x) # 7 x Inception B for i in range(7): x = inception_B(x) # Reduction B x = reduction_B(x) # 3 x Inception C for i in range(3): x = inception_C(x) # Average Pooling x = AveragePooling2D((8, 8))(x) # Dropout x = Dropout(0.8)(x) x = Flatten()(x) # Output out = Dense(output_dim=nb_classes, activation='softmax')(x) model = Model(init, out, name='Inception-v4') if load_weights: if K.backend() == "theano": if K.image_data_format() == "th": weights = get_file( 'inception_v4_weights_th_dim_ordering_th_kernels.h5', TH_BACKEND_TH_DIM_ORDERING, cache_subdir='models') else: weights = get_file( 'inception_v4_weights_tf_dim_ordering_th_kernels.h5', TH_BACKEND_TF_DIM_ORDERING, cache_subdir='models') else: if K.image_data_format() == "th": weights = get_file( 'inception_v4_weights_th_dim_ordering_tf_kernels.h5', TF_BACKEND_TH_DIM_ORDERING, cache_subdir='models') else: weights = get_file( 'inception_v4_weights_tf_dim_ordering_tf_kernels.h5', TH_BACKEND_TF_DIM_ORDERING, cache_subdir='models') model.load_weights(weights) print("Model weights loaded.") return model
def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') #determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor #Block_1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) #Block_2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) #Block_3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) #Block_4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) #Block_5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) #Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input #create model model = Model(inputs, x, name='vgg16') #load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
logdir = sys.argv[1] mkdir(logdir) sys.stdout = open(str(logdir) + '/log.txt', 'w') """Architecture configuration.""" num_blocks = 8 batch_size = 24 input_shape = mpii_sp_dataconf.input_shape num_joints = 16 model = reception.build(input_shape, num_joints, dim=2, num_blocks=num_blocks, num_context_per_joint=2, ksize=(5, 5), concat_pose_confidence=False) """Load pre-trained model.""" weights_path = get_file(weights_file, TF_WEIGHTS_PATH, md5_hash=md5_hash, cache_subdir='models') model.load_weights(weights_path) # model.compile('sgd','mse') """Merge pose and visibility as a single output.""" # """ outputs = [] for b in range(int(len(model.outputs) / 2)): outputs.append(concatenate([model.outputs[2*b], model.outputs[2*b + 1]], name='blk%d' % (b + 1))) # """ # model = Model(model.input, outputs, name=model.name) import matplotlib.pyplot as plt
def ResNet(stack_fn, preact, use_bias, model_name='resnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, classifier_activation='softmax', bottomright_maxpool_test=False, use_group_norm=False, **kwargs): """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. Reference: - [Deep Residual Learning for Image Recognition]( https://arxiv.org/abs/1512.03385) (CVPR 2015) Optionally loads weights pre-trained on ImageNet. Note that the data format convention used by the model is the one specified in your Keras config at `~/.keras/keras.json`. Arguments: stack_fn: a function that returns output tensor for the stacked residual blocks. preact: whether to use pre-activation or not (True for ResNetV2, False for ResNet and ResNeXt). use_bias: whether to use biases for convolutional layers or not (True for ResNet and ResNetV2, False for ResNeXt). model_name: string, model name. include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels. pooling: optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. classifier_activation: A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. **kwargs: For backwards compatibility only. Returns: A `keras.Model` instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. ValueError: if `classifier_activation` is not `softmax` or `None` when using a pretrained top layer. """ global layers if 'layers' in kwargs: layers = kwargs.pop('layers') else: layers = VersionAwareLayers() if kwargs: raise ValueError('Unknown argument(s): %s' % (kwargs, )) if not (weights in {'imagenet', None} or file_io.file_exists_v2(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=224, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1 x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name='conv1_pad')(img_input) x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias and not use_group_norm, name='conv1_conv')(x) if use_group_norm: def norm_layer(name): return tfa.layers.GroupNormalization(epsilon=batchnorm_epsilon, name=name) else: def norm_layer(name): return layers.BatchNormalization(axis=bn_axis, epsilon=batchnorm_epsilon, momentum=batchnorm_momentum, name=name) if not preact: x = norm_layer(name='conv1_gn' if use_group_norm else 'conv1_bn')(x) x = layers.Activation('relu', name='conv1_relu')(x) padding_layer = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='pool1_pad') if bottomright_maxpool_test: padding_test = layers.ZeroPadding2D(padding=((0, 2), (0, 2)), name='pool1_pad') padding_layer = TrainTestSwitchLayer(padding_layer, padding_test) x = padding_layer(x) x = layers.MaxPooling2D(3, strides=2, name='pool1_pool')(x) x = stack_fn(x) if preact: x = norm_layer(name='post_gn' if use_group_norm else 'post_bn')(x) x = layers.Activation('relu', name='post_relu')(x) if include_top: x = layers.GlobalAveragePooling2D(name='avg_pool')(x) imagenet_utils.validate_activation(classifier_activation, weights) x = layers.Dense(classes, activation=classifier_activation, name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D(name='max_pool')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. if use_group_norm: model_name = model_name + '_groupnorm' model = training.Model(inputs, x, name=model_name) # Load weights. if (weights == 'imagenet') and (model_name in WEIGHTS_HASHES): if include_top: file_name = model_name + f'_weights_tf_dim_ordering_tf_kernels.h5' file_hash = WEIGHTS_HASHES[model_name][0] else: file_name = model_name + f'_weights_tf_dim_ordering_tf_kernels_notop.h5' file_hash = WEIGHTS_HASHES[model_name][1] weights_path = data_utils.get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception v3 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299. Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=299, min_size=139, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = Input(tensor=input_tensor, shape=input_shape) if K.image_data_format() == 'channels_first': channel_axis = 1 else: channel_axis = 3 x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid') x = conv2d_bn(x, 32, 3, 3, padding='valid') x = conv2d_bn(x, 64, 3, 3) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv2d_bn(x, 80, 1, 1, padding='valid') x = conv2d_bn(x, 192, 3, 3, padding='valid') x = MaxPooling2D((3, 3), strides=(2, 2))(x) # mixed 0, 1, 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 32, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed0') # mixed 1: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed1') # mixed 2: 35 x 35 x 256 branch1x1 = conv2d_bn(x, 64, 1, 1) branch5x5 = conv2d_bn(x, 48, 1, 1) branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 64, 1, 1) x = layers.concatenate([branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed2') # mixed 3: 17 x 17 x 768 branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid') branch3x3dbl = conv2d_bn(x, 64, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3') # mixed 4: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 128, 1, 1) branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 128, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed4') # mixed 5, 6: 17 x 17 x 768 for i in range(2): branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 160, 1, 1) branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 160, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed' + str(5 + i)) # mixed 7: 17 x 17 x 768 branch1x1 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(x, 192, 1, 1) branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) branch7x7dbl = conv2d_bn(x, 192, 1, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate([branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=channel_axis, name='mixed7') # mixed 8: 8 x 8 x 1280 branch3x3 = conv2d_bn(x, 192, 1, 1) branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid') branch7x7x3 = conv2d_bn(x, 192, 1, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) branch7x7x3 = conv2d_bn(branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid') branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x) x = layers.concatenate([branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8') # mixed 9: 8 x 8 x 2048 for i in range(2): branch1x1 = conv2d_bn(x, 320, 1, 1) branch3x3 = conv2d_bn(x, 384, 1, 1) branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) branch3x3 = layers.concatenate([branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i)) branch3x3dbl = conv2d_bn(x, 448, 1, 1) branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x) branch_pool = conv2d_bn(branch_pool, 192, 1, 1) x = layers.concatenate( [branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed' + str(9 + i)) if include_top: # Classification block x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='inception_v3') # load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='9a0d58056eeedaa3f26cb7ebd46da564') else: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='bcbd6486424b2319ff4ef7d526e38f63') model.load_weights(weights_path) if K.backend() == 'theano': convert_all_kernels_in_model(model) return model
def Deeplabv3(weights=None, input_tensor=None, input_shape=(256, 256, 4), classes=2, backbone='xception', OS=16, alpha=1.): """ Instantiates the Deeplabv3+ architecture Optionally loads weights pre-trained on PASCAL VOC. This model is available for TensorFlow only, and can only be used with inputs following the TensorFlow data format `(width, height, channels)`. # Arguments weights: one of 'pascal_voc' (pre-trained on pascal voc) or None (random initialization) input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: shape of input image. format HxWxC PASCAL VOC model was trained on (512,512,3) images classes: number of desired classes. If classes != 21, last layer is initialized randomly backbone: backbone to use. one of {'xception','mobilenetv2'} OS: determines input_shape/feature_extractor_output ratio. One of {8,16}. Used only for xception backbone. alpha: controls the width of the MobileNetV2 network. This is known as the width multiplier in the MobileNetV2 paper. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. Used only for mobilenetv2 backbone # Returns A Keras model instance. # Raises RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. ValueError: in case of invalid argument for `weights` or `backbone` """ if not (weights in {'pascal_voc', None}): raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `pascal_voc` ' '(pre-trained on PASCAL VOC)') if K.backend() != 'tensorflow': raise RuntimeError('The Deeplabv3+ model is only available with ' 'the TensorFlow backend.') if not (backbone in {'xception', 'mobilenetv2'}): raise ValueError('The `backbone` argument should be either ' '`xception` or `mobilenetv2` ') if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if backbone == 'xception': if OS == 8: entry_block3_stride = 1 middle_block_rate = 2 # ! Not mentioned in paper, but required exit_block_rates = (2, 4) atrous_rates = (12, 24, 36) else: entry_block3_stride = 2 middle_block_rate = 1 exit_block_rates = (1, 2) atrous_rates = (6, 12, 18) x = Conv2D(32, (3, 3), strides=(2, 2), name='entry_flow_conv1_1', use_bias=False, padding='same')(img_input) x = BatchNormalization(name='entry_flow_conv1_1_BN')(x) x = Activation('relu')(x) x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1) x = BatchNormalization(name='entry_flow_conv1_2_BN')(x) x = Activation('relu')(x) x = _xception_block(x, [128, 128, 128], 'entry_flow_block1', skip_connection_type='conv', stride=2, depth_activation=False) x, skip1 = _xception_block(x, [256, 256, 256], 'entry_flow_block2', skip_connection_type='conv', stride=2, depth_activation=False, return_skip=True) x = _xception_block(x, [728, 728, 728], 'entry_flow_block3', skip_connection_type='conv', stride=entry_block3_stride, depth_activation=False) for i in range(16): x = _xception_block(x, [728, 728, 728], 'middle_flow_unit_{}'.format(i + 1), skip_connection_type='sum', stride=1, rate=middle_block_rate, depth_activation=False) x = _xception_block(x, [728, 1024, 1024], 'exit_flow_block1', skip_connection_type='conv', stride=1, rate=exit_block_rates[0], depth_activation=False) x = _xception_block(x, [1536, 1536, 2048], 'exit_flow_block2', skip_connection_type='none', stride=1, rate=exit_block_rates[1], depth_activation=True) else: OS = 8 first_block_filters = _make_divisible(32 * alpha, 8) x = Conv2D(first_block_filters, kernel_size=3, strides=(2, 2), padding='same', use_bias=False, name='Conv')(img_input) x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x) x = Activation(relu6, name='Conv_Relu6')(x) x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0, skip_connection=False) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1, skip_connection=False) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2, skip_connection=True) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3, skip_connection=False) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4, skip_connection=True) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5, skip_connection=True) # stride in block 6 changed from 2 -> 1, so we need to use rate = 2 x = _inverted_res_block( x, filters=64, alpha=alpha, stride=1, # 1! expansion=6, block_id=6, skip_connection=False) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2, expansion=6, block_id=7, skip_connection=True) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2, expansion=6, block_id=8, skip_connection=True) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2, expansion=6, block_id=9, skip_connection=True) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2, expansion=6, block_id=10, skip_connection=False) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2, expansion=6, block_id=11, skip_connection=True) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2, expansion=6, block_id=12, skip_connection=True) x = _inverted_res_block( x, filters=160, alpha=alpha, stride=1, rate=2, # 1! expansion=6, block_id=13, skip_connection=False) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4, expansion=6, block_id=14, skip_connection=True) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4, expansion=6, block_id=15, skip_connection=True) x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, rate=4, expansion=6, block_id=16, skip_connection=False) # end of feature extractor # branching for Atrous Spatial Pyramid Pooling # Image Feature branch #out_shape = int(np.ceil(input_shape[0] / OS)) b4 = AveragePooling2D(pool_size=(int(np.ceil(input_shape[0] / OS)), int(np.ceil(input_shape[1] / OS))))(x) b4 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='image_pooling')(b4) b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4) b4 = Activation('relu')(b4) b4 = BilinearUpsampling((int(np.ceil(input_shape[0] / OS)), int(np.ceil(input_shape[1] / OS))))(b4) # simple 1x1 b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x) b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0) b0 = Activation('relu', name='aspp0_activation')(b0) # there are only 2 branches in mobilenetV2. not sure why if backbone == 'xception': # rate = 6 (12) b1 = SepConv_BN(x, 256, 'aspp1', rate=atrous_rates[0], depth_activation=True, epsilon=1e-5) # rate = 12 (24) b2 = SepConv_BN(x, 256, 'aspp2', rate=atrous_rates[1], depth_activation=True, epsilon=1e-5) # rate = 18 (36) b3 = SepConv_BN(x, 256, 'aspp3', rate=atrous_rates[2], depth_activation=True, epsilon=1e-5) # concatenate ASPP branches & project x = Concatenate()([b4, b0, b1, b2, b3]) else: x = Concatenate()([b4, b0]) x = Conv2D(256, (1, 1), padding='same', use_bias=False, name='concat_projection')(x) x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x) x = Activation('relu')(x) x = Dropout(0.1)(x) # DeepLab v.3+ decoder if backbone == 'xception': # Feature projection # x4 (x2) block x = BilinearUpsampling(output_size=(int(np.ceil(input_shape[0] / 4)), int(np.ceil(input_shape[1] / 4))))(x) dec_skip1 = Conv2D(48, (1, 1), padding='same', use_bias=False, name='feature_projection0')(skip1) dec_skip1 = BatchNormalization(name='feature_projection0_BN', epsilon=1e-5)(dec_skip1) dec_skip1 = Activation('relu')(dec_skip1) x = Concatenate()([x, dec_skip1]) x = SepConv_BN(x, 256, 'decoder_conv0', depth_activation=True, epsilon=1e-5) x = SepConv_BN(x, 256, 'decoder_conv1', depth_activation=True, epsilon=1e-5) # you can use it with arbitary number of classes if classes == 21: last_layer_name = 'logits_semantic' else: last_layer_name = 'custom_logits_semantic' x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x) x = BilinearUpsampling(output_size=(input_shape[0], input_shape[1]))(x) x = Reshape((classes, -1))(x) x = Permute((2, 1))(x) x = Activation('softmax')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input model = Model(inputs, x, name='deeplabv3plus') # load weights if weights == 'pascal_voc': if backbone == 'xception': weights_path = get_file( 'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_X, cache_subdir='models') else: weights_path = get_file( 'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_MOBILE, cache_subdir='models') model.load_weights(weights_path, by_name=True) return model
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=(224, 224, 3), pooling="max", classes=2): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def _maybe_download(url, fname, md5_hash): origin = os.path.join(url, fname) fpath = get_file(fname, origin=origin, untar=False, md5_hash=md5_hash) return fpath
def Stylize(arg_base_image_path, arg_style_image_path, arg_result_prefix, arg_style_masks=None, arg_color_mask=None, arg_rescale_image=False, arg_maintain_aspect_ratio=True, arg_color=False, arg_content_weight=0.025, arg_tv_weight=8.5e-5, arg_style_weight=[1], arg_style_scale=1.0, arg_pool="max", arg_init_image="content", arg_content_loss_type=0, arg_img_size=400, arg_model="vgg16", arg_content_layer="conv5_2", arg_num_iter=10, arg_min_improvement=0.0, arg_rescale_method="bilinear"): """ Neural Style Transfer with Keras 1.1.2 Based on: https://github.com/fchollet/keras/blob/master/examples/neural_style_transfer.py ----------------------------------------------------------------------------------------------------------------------- """ THEANO_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels_notop.h5' TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' TH_19_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_th_dim_ordering_th_kernels_notop.h5' TF_19_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5' ''' Arguments ''' base_image_path = arg_base_image_path style_reference_image_paths = [arg_style_image_path ] #arg_style_image_paths result_prefix = arg_result_prefix style_image_paths = [] for style_image_path in style_reference_image_paths: style_image_paths.append(style_image_path) style_masks_present = arg_style_masks is not None mask_paths = [] if style_masks_present: for mask_path in arg_style_masks: mask_paths.append(mask_path) if style_masks_present: assert len(style_image_paths) == len(mask_paths), "Wrong number of style masks provided.\n" \ "Number of style images = %d, \n" \ "Number of style mask paths = %d." % \ (len(style_image_paths), len(style_masks_present)) color_mask_present = arg_color_mask is not None rescale_image = arg_rescale_image maintain_aspect_ratio = arg_maintain_aspect_ratio preserve_color = arg_color # these are the weights of the different loss components content_weight = arg_content_weight total_variation_weight = arg_tv_weight style_weights = [] if len(style_image_paths) != len(arg_style_weight): print( "Mismatch in number of style images provided and number of style weights provided. \n" "Found %d style images and %d style weights. \n" "Equally distributing weights to all other styles." % (len(style_image_paths), len(arg_style_weight))) weight_sum = sum(arg_style_weight) * arg_style_scale count = len(style_image_paths) for i in range(len(style_image_paths)): style_weights.append(weight_sum / count) else: for style_weight in arg_style_weight: style_weights.append(style_weight * arg_style_scale) # Decide pooling function pooltype = str(arg_pool).lower() assert pooltype in [ "ave", "max" ], 'Pooling argument is wrong. Needs to be either "ave" or "max".' pooltype = 1 if pooltype == "ave" else 0 read_mode = "gray" if arg_init_image == "gray" else "color" # dimensions of the generated picture. img_width = img_height = 0 img_WIDTH = base_image_path.shape[0] img_HEIGHT = base_image_path.shape[1] aspect_ratio = float(img_HEIGHT) / img_WIDTH img_width = arg_img_size if maintain_aspect_ratio: img_height = int(img_width * aspect_ratio) else: img_height = arg_img_size assert arg_content_loss_type in [ 0, 1, 2 ], "Content Loss Type must be one of 0, 1 or 2" # util function to open, resize and format pictures into appropriate tensors def preprocess_image(image_path, load_dims=False, read_mode="color"): #global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio mode = "RGB" if read_mode == "color" else "L" img = imread(image_path, mode=mode) # Prevents crashes due to PNG images (ARGB) if mode == "L": # Expand the 1 channel grayscale to 3 channel grayscale image temp = np.zeros(img.shape + (3, ), dtype=np.uint8) temp[:, :, 0] = img temp[:, :, 1] = img.copy() temp[:, :, 2] = img.copy() img = temp # if load_dims: # img_WIDTH = img.shape[0] # img_HEIGHT = img.shape[1] # aspect_ratio = float(img_HEIGHT) / img_WIDTH # img_width = arg_img_size # if maintain_aspect_ratio: # img_height = int(img_width * aspect_ratio) # else: # img_height = arg_img_size img = imresize(img, (img_width, img_height)).astype('float32') # RGB -> BGR img = img[:, :, ::-1] img[:, :, 0] -= 103.939 img[:, :, 1] -= 116.779 img[:, :, 2] -= 123.68 if K.image_dim_ordering() == "th": img = img.transpose((2, 0, 1)).astype('float32') img = np.expand_dims(img, axis=0) return img def preprocess_image_wo_path(image, load_dims=True): #global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio img = image #print ("in preprocess_image width x height: ",img_width,img_height) img = imresize(img, (img_width, img_height)).astype('float32') # RGB -> BGR img = img[:, :, ::-1] img[:, :, 0] -= 103.939 img[:, :, 1] -= 116.779 img[:, :, 2] -= 123.68 if K.image_dim_ordering() == "th": img = img.transpose((2, 0, 1)).astype('float32') img = np.expand_dims(img, axis=0) return img # util function to convert a tensor into a valid image def deprocess_image(x): if K.image_dim_ordering() == "th": x = x.reshape((3, img_width, img_height)) x = x.transpose((1, 2, 0)) else: x = x.reshape((img_width, img_height, 3)) x[:, :, 0] += 103.939 x[:, :, 1] += 116.779 x[:, :, 2] += 123.68 # BGR -> RGB x = x[:, :, ::-1] x = np.clip(x, 0, 255).astype('uint8') return x # util function to preserve image color def original_color_transform(content, generated, mask=None): generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space if mask is None: generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr else: width, height, channels = generated.shape for i in range(width): for j in range(height): if mask[i, j] == 1: generated[i, j, 1:] = content[i, j, 1:] generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space return generated def load_mask(mask_path, shape, return_mask_img=False): if K.image_dim_ordering() == "th": _, channels, width, height = shape else: _, width, height, channels = shape mask = imread(mask_path, mode="L") # Grayscale mask load mask = imresize(mask, (width, height)).astype('float32') # Perform binarization of mask mask[mask <= 127] = 0 mask[mask > 128] = 255 max = np.amax(mask) mask /= max if return_mask_img: return mask mask_shape = shape[1:] mask_tensor = np.empty(mask_shape) for i in range(channels): if K.image_dim_ordering() == "th": mask_tensor[i, :, :] = mask else: mask_tensor[:, :, i] = mask return mask_tensor def pooling_func(x): if pooltype == 1: return AveragePooling2D((2, 2), strides=(2, 2))(x) else: return MaxPooling2D((2, 2), strides=(2, 2))(x) # get tensor representations of our images base_image = K.variable(preprocess_image_wo_path(base_image_path, True)) print("width x height: ", img_width, img_height) style_reference_images = [] for style_path in style_image_paths: style_reference_images.append(K.variable(preprocess_image(style_path))) print("width x height: ", img_width, img_height) # this will contain our generated image if K.image_dim_ordering() == 'th': combination_image = K.placeholder((1, 3, img_width, img_height)) else: combination_image = K.placeholder((1, img_width, img_height, 3)) image_tensors = [base_image] for style_image_tensor in style_reference_images: image_tensors.append(style_image_tensor) image_tensors.append(combination_image) nb_tensors = len(image_tensors) nb_style_images = nb_tensors - 2 # Content and Output image not considered print("nb_tensor ", nb_tensors) print("nb_style_images ", nb_style_images) print("image_tensors.shape ", np.array(image_tensors).shape) # combine the various images into a single Keras tensor input_tensor = K.concatenate(image_tensors, axis=0) if K.image_dim_ordering() == "th": shape = (nb_tensors, 3, img_width, img_height) else: shape = (nb_tensors, img_width, img_height, 3) ip = Input(tensor=input_tensor, shape=shape) # build the VGG16 network with our 3 images as input x = Convolution2D(64, 3, 3, activation='relu', name='conv1_1', border_mode='same')(ip) x = Convolution2D(64, 3, 3, activation='relu', name='conv1_2', border_mode='same')(x) x = pooling_func(x) x = Convolution2D(128, 3, 3, activation='relu', name='conv2_1', border_mode='same')(x) x = Convolution2D(128, 3, 3, activation='relu', name='conv2_2', border_mode='same')(x) x = pooling_func(x) x = Convolution2D(256, 3, 3, activation='relu', name='conv3_1', border_mode='same')(x) x = Convolution2D(256, 3, 3, activation='relu', name='conv3_2', border_mode='same')(x) x = Convolution2D(256, 3, 3, activation='relu', name='conv3_3', border_mode='same')(x) if arg_model == "vgg19": x = Convolution2D(256, 3, 3, activation='relu', name='conv3_4', border_mode='same')(x) x = pooling_func(x) x = Convolution2D(512, 3, 3, activation='relu', name='conv4_1', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', name='conv4_2', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', name='conv4_3', border_mode='same')(x) if arg_model == "vgg19": x = Convolution2D(512, 3, 3, activation='relu', name='conv4_4', border_mode='same')(x) x = pooling_func(x) x = Convolution2D(512, 3, 3, activation='relu', name='conv5_1', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', name='conv5_2', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', name='conv5_3', border_mode='same')(x) if arg_model == "vgg19": x = Convolution2D(512, 3, 3, activation='relu', name='conv5_4', border_mode='same')(x) x = pooling_func(x) model = Model(ip, x) if K.image_dim_ordering() == "th": if arg_model == "vgg19": weights = get_file( 'vgg19_weights_th_dim_ordering_th_kernels_notop.h5', TH_19_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: weights = get_file( 'vgg16_weights_th_dim_ordering_th_kernels_notop.h5', THEANO_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: if arg_model == "vgg19": weights = get_file( 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_19_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: weights = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights) if K.backend() == 'tensorflow' and K.image_dim_ordering() == "th": warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image dimension ordering convention ' '(`image_dim_ordering="th"`). ' 'For best performance, set ' '`image_dim_ordering="tf"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') convert_all_kernels_in_model(model) print('Model loaded.') # get the symbolic outputs of each "key" layer (we gave them unique names). outputs_dict = dict([(layer.name, layer.output) for layer in model.layers]) shape_dict = dict([(layer.name, layer.output_shape) for layer in model.layers]) # compute the neural style loss # first we need to define 4 util functions # the gram matrix of an image tensor (feature-wise outer product) def gram_matrix(x): assert K.ndim(x) == 3 if K.image_dim_ordering() == "th": features = K.batch_flatten(x) else: features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) gram = K.dot(features, K.transpose(features)) return gram # the "style loss" is designed to maintain # the style of the reference image in the generated image. # It is based on the gram matrices (which capture style) of # feature maps from the style reference image # and from the generated image def style_loss(style, combination, mask_path=None, nb_channels=None): assert K.ndim(style) == 3 assert K.ndim(combination) == 3 if mask_path is not None: style_mask = load_mask(mask_path, nb_channels) style = style * style_mask combination = combination * style_mask del style_mask S = gram_matrix(style) C = gram_matrix(combination) channels = 3 size = img_width * img_height return K.sum(K.square(S - C)) / (4. * (channels**2) * (size**2)) # an auxiliary loss function # designed to maintain the "content" of the # base image in the generated image def content_loss(base, combination): channel_dim = 0 if K.image_dim_ordering() == "th" else -1 channels = K.shape(base)[channel_dim] size = img_width * img_height if arg_content_loss_type == 1: multiplier = 1 / (2. * channels**0.5 * size**0.5) elif arg_content_loss_type == 2: multiplier = 1 / (channels * size) else: multiplier = 1. return multiplier * K.sum(K.square(combination - base)) # the 3rd loss function, total variation loss, # designed to keep the generated image locally coherent def total_variation_loss(x): assert K.ndim(x) == 4 if K.image_dim_ordering() == 'th': a = K.square(x[:, :, :img_width - 1, :img_height - 1] - x[:, :, 1:, :img_height - 1]) b = K.square(x[:, :, :img_width - 1, :img_height - 1] - x[:, :, :img_width - 1, 1:]) else: a = K.square(x[:, :img_width - 1, :img_height - 1, :] - x[:, 1:, :img_height - 1, :]) b = K.square(x[:, :img_width - 1, :img_height - 1, :] - x[:, :img_width - 1, 1:, :]) return K.sum(K.pow(a + b, 1.25)) # combine these loss functions into a single scalar loss = K.variable(0.) layer_features = outputs_dict[arg_content_layer] # 'conv5_2' or 'conv4_2' base_image_features = layer_features[0, :, :, :] combination_features = layer_features[nb_tensors - 1, :, :, :] loss += content_weight * content_loss(base_image_features, combination_features) style_masks = [] if style_masks_present: style_masks = mask_paths # If mask present, pass dictionary of masks to style loss else: style_masks = [None for _ in range(nb_style_images) ] # If masks not present, pass None to the style loss channel_index = 1 if K.image_dim_ordering() == "th" else -1 feature_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] for layer_name in feature_layers: layer_features = outputs_dict[layer_name] shape = shape_dict[layer_name] combination_features = layer_features[nb_tensors - 1, :, :, :] style_reference_features = layer_features[1:nb_tensors - 1, :, :, :] sl = [] for j in range(nb_style_images): sl.append( style_loss(style_reference_features[j], combination_features, style_masks[j], shape)) for j in range(nb_style_images): loss += (style_weights[j] / len(feature_layers)) * sl[j] loss += total_variation_weight * total_variation_loss(combination_image) # get the gradients of the generated image wrt the loss grads = K.gradients(loss, combination_image) outputs = [loss] if type(grads) in {list, tuple}: outputs += grads else: outputs.append(grads) f_outputs = K.function([combination_image], outputs) def eval_loss_and_grads(x): if K.image_dim_ordering() == 'th': x = x.reshape((1, 3, img_width, img_height)) else: x = x.reshape((1, img_width, img_height, 3)) outs = f_outputs([x]) loss_value = outs[0] if len(outs[1:]) == 1: grad_values = outs[1].flatten().astype('float64') else: grad_values = np.array(outs[1:]).flatten().astype('float64') return loss_value, grad_values # this Evaluator class makes it possible # to compute loss and gradients in one pass # while retrieving them via two separate functions, # "loss" and "grads". This is done because scipy.optimize # requires separate functions for loss and gradients, # but computing them separately would be inefficient. class Evaluator(object): def __init__(self): self.loss_value = None self.grads_values = None def loss(self, x): assert self.loss_value is None loss_value, grad_values = eval_loss_and_grads(x) self.loss_value = loss_value self.grad_values = grad_values return self.loss_value def grads(self, x): assert self.loss_value is not None grad_values = np.copy(self.grad_values) self.loss_value = None self.grad_values = None return grad_values evaluator = Evaluator() # run scipy-based optimization (L-BFGS) over the pixels of the generated image # so as to minimize the neural style loss if "content" in arg_init_image or "gray" in arg_init_image: x = preprocess_image_wo_path(base_image_path, True) elif "noise" in arg_init_image: x = np.random.uniform(0, 255, (1, img_width, img_height, 3)) - 128. if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) else: print("Using initial image : ", arg_init_image) x = preprocess_image(arg_init_image, read_mode=read_mode) # We require original image if we are to preserve color in YCbCr mode if preserve_color: #content = imread(base_image_path, mode="YCbCr") content = cv2.cvtColor(base_image_path, cv2.COLOR_RGB2YCrCb) content = imresize(content, (img_width, img_height)) if color_mask_present: if K.image_dim_ordering() == "th": color_mask_shape = (None, None, img_width, img_height) else: color_mask_shape = (None, img_width, img_height, None) color_mask = load_mask(arg_color_mask, color_mask_shape, return_mask_img=True) else: color_mask = None else: color_mask = None num_iter = arg_num_iter prev_min_val = -1 improvement_threshold = float(arg_min_improvement) for i in range(num_iter): print("Starting iteration %d of %d" % ((i + 1), num_iter)) start_time = time.time() x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(), fprime=evaluator.grads, maxfun=20) if prev_min_val == -1: prev_min_val = min_val improvement = (prev_min_val - min_val) / prev_min_val * 100 print('Current loss value:', min_val, " Improvement : %0.3f" % improvement, "%") prev_min_val = min_val # save current generated image img = deprocess_image(x.copy()) if preserve_color and content is not None: img = original_color_transform(content, img, mask=color_mask) if not rescale_image: img_ht = int(img_width * aspect_ratio) print("width x height : aspect_ratio == ", img_width, img_height, aspect_ratio) # img_ht = img_height print("Rescaling Image to (%d, %d)" % (img_width, img_ht)) img = imresize(img, (img_width, img_ht), interp=arg_rescale_method) if rescale_image: print("Rescaling Image to (%d, %d)" % (img_WIDTH, img_HEIGHT)) img = imresize(img, (img_WIDTH, img_HEIGHT), interp=arg_rescale_method) fname = result_prefix + '_at_iteration_%d.png' % (i + 1) end_time = time.time() print('Iteration %d completed in %ds' % (i + 1, end_time - start_time)) #return the image img #imsave(fname, img) return img
def VGG16(include_top=True, weights='vggface', input_tensor=None, input_shape=None, pooling=None, classes=2622): input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_1')( img_input) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='conv1_2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_1')( x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='conv2_2')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool2')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_1')( x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_2')( x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='conv3_3')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool3')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_1')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_2')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv4_3')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool4')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_1')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_2')( x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='conv5_3')( x) x = MaxPooling2D((2, 2), strides=(2, 2), name='pool5')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, name='fc6')(x) x = Activation('relu', name='fc6/relu')(x) x = Dense(4096, name='fc7')(x) x = Activation('relu', name='fc7/relu')(x) x = Dense(classes, name='fc8')(x) x = Activation('softmax', name='fc8/softmax')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vggface_vgg16') # load weights if weights == 'vggface': if include_top: weights_path = get_file('rcmalli_vggface_tf_vgg16.h5', utils. VGG16_WEIGHTS_PATH, cache_subdir=utils.VGGFACE_DIR) else: weights_path = get_file('rcmalli_vggface_tf_notop_vgg16.h5', utils.VGG16_WEIGHTS_PATH_NO_TOP, cache_subdir=utils.VGGFACE_DIR) model.load_weights(weights_path, by_name=True) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='pool5') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc6') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def NASNet(input_shape=None, penultimate_filters=4032, nb_blocks=6, stem_filters=96, skip_reduction=True, use_auxiliary_branch=False, filters_multiplier=2, dropout=0.5, weight_decay=5e-5, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000, default_size=None): """Instantiates a NASNet architecture. Note that only TensorFlow is supported for now, therefore it only works with the data format `image_data_format='channels_last'` in your Keras config at `~/.keras/keras.json`. # Arguments input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(331, 331, 3)` for NASNetLarge or `(224, 224, 3)` for NASNetMobile It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(224, 224, 3)` would be one valid value. penultimate_filters: number of filters in the penultimate layer. NASNet models use the notation `NASNet (N @ P)`, where: - N is the number of blocks - P is the number of penultimate filters nb_blocks: number of repeated blocks of the NASNet model. NASNet models use the notation `NASNet (N @ P)`, where: - N is the number of blocks - P is the number of penultimate filters stem_filters: number of filters in the initial stem block skip_reduction: Whether to skip the reduction step at the tail end of the network. Set to `False` for CIFAR models. use_auxiliary_branch: Whether to use the auxiliary branch during training or evaluation. filters_multiplier: controls the width of the network. - If `filters_multiplier` < 1.0, proportionally decreases the number of filters in each layer. - If `filters_multiplier` > 1.0, proportionally increases the number of filters in each layer. - If `filters_multiplier` = 1, default number of filters from the paper are used at each layer. dropout: dropout rate weight_decay: l2 regularization weight include_top: whether to include the fully-connected layer at the top of the network. weights: `None` (random initialization) or `imagenet` (ImageNet weights) input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. default_size: specifies the default image size of the model # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. """ if K.backend() != 'tensorflow': raise RuntimeError('Only Tensorflow backend is currently supported, ' 'as other backends do not support ' 'separable convolution.') if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as ImageNet with `include_top` ' 'as true, `classes` should be 1000') if default_size is None: default_size = 331 # Determine proper input shape and default size. input_shape = _obtain_input_shape(input_shape, default_size=default_size, min_size=32, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if K.image_data_format() != 'channels_last': warnings.warn('The NASNet family of models is only available ' 'for the input data format "channels_last" ' '(width, height, channels). ' 'However your settings specify the default ' 'data format "channels_first" (channels, width, height).' ' You should set `image_data_format="channels_last"` ' 'in your Keras config located at ~/.keras/keras.json. ' 'The model being returned right now will expect inputs ' 'to follow the "channels_last" data format.') K.set_image_data_format('channels_last') old_data_format = 'channels_first' else: old_data_format = None if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor assert penultimate_filters % 24 == 0, "`penultimate_filters` needs to be divisible " \ "by 24." channel_dim = 1 if K.image_data_format() == 'channels_first' else -1 filters = penultimate_filters // 24 if not skip_reduction: x = Conv2D(stem_filters, (3, 3), strides=(2, 2), padding='valid', use_bias=False, name='stem_conv1', kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input) else: x = Conv2D(stem_filters, (3, 3), strides=(1, 1), padding='same', use_bias=False, name='stem_conv1', kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(img_input) x = BatchNormalization(axis=channel_dim, momentum=_BN_DECAY, epsilon=_BN_EPSILON, name='stem_bn1')(x) p = None if not skip_reduction: # imagenet / mobile mode x, p = _reduction_A(x, p, filters // (filters_multiplier ** 2), weight_decay, id='stem_1') x, p = _reduction_A(x, p, filters // filters_multiplier, weight_decay, id='stem_2') for i in range(nb_blocks): x, p = _normal_A(x, p, filters, weight_decay, id='%d' % (i)) x, p0 = _reduction_A(x, p, filters * filters_multiplier, weight_decay, id='reduce_%d' % (nb_blocks)) p = p0 if not skip_reduction else p for i in range(nb_blocks): x, p = _normal_A(x, p, filters * filters_multiplier, weight_decay, id='%d' % (nb_blocks + i + 1)) auxiliary_x = None if not skip_reduction: # imagenet / mobile mode if use_auxiliary_branch: auxiliary_x = _add_auxiliary_head(x, classes, weight_decay) x, p0 = _reduction_A(x, p, filters * filters_multiplier ** 2, weight_decay, id='reduce_%d' % (2 * nb_blocks)) if skip_reduction: # CIFAR mode if use_auxiliary_branch: auxiliary_x = _add_auxiliary_head(x, classes, weight_decay) p = p0 if not skip_reduction else p for i in range(nb_blocks): x, p = _normal_A(x, p, filters * filters_multiplier ** 2, weight_decay, id='%d' % (2 * nb_blocks + i + 1)) x = Activation('relu')(x) if include_top: x = GlobalAveragePooling2D()(x) x = Dropout(dropout)(x) x = Dense(classes, activation='softmax', kernel_regularizer=l2(weight_decay), name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. if use_auxiliary_branch: model = Model(inputs, [x, auxiliary_x], name='NASNet_with_auxiliary') else: model = Model(inputs, x, name='NASNet') # load weights if weights == 'imagenet': if default_size == 224: # mobile version if include_top: if use_auxiliary_branch: weight_path = NASNET_MOBILE_WEIGHT_PATH_WITH_AUXULARY model_name = 'nasnet_mobile_with_aux.h5' else: weight_path = NASNET_MOBILE_WEIGHT_PATH model_name = 'nasnet_mobile.h5' else: if use_auxiliary_branch: weight_path = NASNET_MOBILE_WEIGHT_PATH_WITH_AUXULARY_NO_TOP model_name = 'nasnet_mobile_with_aux_no_top.h5' else: weight_path = NASNET_MOBILE_WEIGHT_PATH_NO_TOP model_name = 'nasnet_mobile_no_top.h5' weights_file = get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_file, by_name=True) elif default_size == 331: # large version if include_top: if use_auxiliary_branch: weight_path = NASNET_LARGE_WEIGHT_PATH_WITH_auxiliary model_name = 'nasnet_large_with_aux.h5' else: weight_path = NASNET_LARGE_WEIGHT_PATH model_name = 'nasnet_large.h5' else: if use_auxiliary_branch: weight_path = NASNET_LARGE_WEIGHT_PATH_WITH_auxiliary_NO_TOP model_name = 'nasnet_large_with_aux_no_top.h5' else: weight_path = NASNET_LARGE_WEIGHT_PATH_NO_TOP model_name = 'nasnet_large_no_top.h5' weights_file = get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_file, by_name=True) else: raise ValueError('ImageNet weights can only be loaded on NASNetLarge or NASNetMobile') if old_data_format: K.set_image_data_format(old_data_format) return model
def ResNet152(include_top=True, weights=None, input_tensor=None, input_shape=None, large_input=False, pooling=None, classes=1000): """Instantiate the ResNet152 architecture. Keyword arguments: include_top -- whether to include the fully-connected layer at the top of the network. (default True) weights -- one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). (default None) input_tensor -- optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model.(default None) input_shape -- optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 224)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. E.g. `(200, 200, 3)` would be one valid value. (default None) large_input -- if True, then the input shape expected will be `(448, 448, 3)` (with `channels_last` data format) or `(3, 448, 448)` (with `channels_first` data format). (default False) pooling -- Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. (default None) classes -- optional number of classes to classify image into, only to be specified if `include_top` is True, and if no `weights` argument is specified. (default 1000) Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') eps = 1.1e-5 if large_input: img_size = 448 else: img_size = 224 # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=img_size, min_size=197, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # handle dimension ordering for different backends if K.image_dim_ordering() == 'tf': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=False)(x) x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x) x = Scale(axis=bn_axis, name='scale_conv1')(x) x = Activation('relu', name='conv1_relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') for i in range(1, 8): x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i)) x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') for i in range(1, 36): x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i)) x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') if large_input: x = AveragePooling2D((14, 14), name='avg_pool')(x) else: x = AveragePooling2D((7, 7), name='avg_pool')(x) # include classification layer by default, not included for feature extraction if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet152') # load weights if weights == 'imagenet': if include_top: weights_path = get_file('resnet152_weights_tf.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='cdb18a2158b88e392c0905d47dcef965') else: weights_path = get_file('resnet152_weights_tf_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='4a90dcdafacbd17d772af1fb44fc2660') model.load_weights(weights_path, by_name=True) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model
def baseline_model(include_top=True, weights='HRA', input_tensor=None, input_shape=None, classes=9, epochs=40): """Instantiates a baseline deep CNN architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format='channels_last'` in your Keras config at ~/.keras/keras.json. # Arguments include_top: whether to include the 2 fully-connected layers at the top of the network. weights: one of `None` (random initialization), 'HRA' (pre-training on Human Rights Archive), or the path to the weights file to be loaded. input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. epochs: Integer. Number of epochs to train the model. augmented_samples: whether to use the augmented samples during training # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if not (weights in {'HRA', None} or os.path.exists(weights)): raise ValueError('The `weights_top_layers` argument should be either ' '`None` (random initialization), `HRA` ' '(pre-training on Human Rights Archive), ' 'or the path to the weights file to be loaded.') if weights == 'HRA' and include_top and classes != 9: raise ValueError('If using `weights` as Human Rights Archive, `classes` should be 9.') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Define The Neural Network Model # Block 1 x = Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(32, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # the model so far outputs 3D feature maps (height, width, features) # On top of it we stick two fully-connected layers. We end the model with 9 final outputs representing probabilities for HRA classes # and a softmax activation, which is perfect for a multi-class classification. # To go with it we will also use the categorical_crossentropy loss to train our model. if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(64, activation='relu', name='fc1')(x) x = Dropout(0.5)(x) x = Dense(classes, activation='softmax', name='predictions')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='HRA-baseline_convnet') model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) model.summary() # load weights if weights == 'HRA' and epochs == 10: if include_top: weights_path = get_file('cost_sensitive_baseline_10_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_10_EPOCHS, cache_subdir='HRA_models') elif weights == 'HRA' and epochs == 20: if include_top: weights_path = get_file('cost_sensitive_baseline_20_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_20_EPOCHS, cache_subdir='HRA_models') elif weights == 'HRA' and epochs == 40: if include_top: weights_path = get_file('cost_sensitive_baseline_40_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_40_EPOCHS, cache_subdir='HRA_models') model.load_weights(weights_path) return model
def Deeplabv3(input_shape, num_classes, load_weights_from_internet=False, regularizer=None, weights='pascal_voc', input_tensor=None, backbone='mobilenetv2', OS=16, alpha=1., activation='softmax'): """ Instantiates the Deeplabv3+ architecture Optionally loads weights pre-trained on PASCAL VOC or Cityscapes. This model is available for TensorFlow only. # Arguments weights: one of 'pascal_voc' (pre-trained on pascal voc), 'cityscapes' (pre-trained on cityscape) or None (random initialization) input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: shape of input image. format HxWxC PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width num_classes: number of desired classes. PASCAL VOC has 21 classes, Cityscapes has 19 classes. If number of classes not aligned with the weights used, last layer is initialized randomly backbone: backbone to use. one of {'xception','mobilenetv2'} activation: optional activation to add to the top of the network. One of 'softmax', 'sigmoid' or None OS: determines input_shape/feature_extractor_output ratio. One of {8,16}. Used only for xception backbone. alpha: controls the width of the MobileNetV2 network. This is known as the width multiplier in the MobileNetV2 paper. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1. # Returns A Keras model instance. # Raises RuntimeError: If attempting to run this model with a backend that does not support separable convolutions. ValueError: in case of invalid argument for `weights` or `backbone` """ if not (weights in {'pascal_voc', 'cityscapes', None}): raise ValueError( 'The `weights` argument should be either ' '`None` (random initialization), `pascal_voc`, or `cityscapes` ' '(pre-trained on PASCAL VOC)') if not (backbone in {'xception', 'mobilenetv2'}): raise ValueError('The `backbone` argument should be either ' '`xception` or `mobilenetv2` ') if input_tensor is None: img_input = Input(shape=input_shape) else: img_input = input_tensor if backbone == 'xception': if OS == 8: entry_block3_stride = 1 middle_block_rate = 2 # ! Not mentioned in paper, but required exit_block_rates = (2, 4) atrous_rates = (12, 24, 36) else: entry_block3_stride = 2 middle_block_rate = 1 exit_block_rates = (1, 2) atrous_rates = (6, 12, 18) x = Conv2D(32, (3, 3), strides=(2, 2), kernel_regularizer=regularizer, name='entry_flow_conv1_1', use_bias=False, padding='same')(img_input) x = BatchNormalization(name='entry_flow_conv1_1_BN')(x) x = Activation(tf.nn.relu)(x) x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1) x = BatchNormalization(name='entry_flow_conv1_2_BN')(x) x = Activation(tf.nn.relu)(x) x = _xception_block(x, [128, 128, 128], 'entry_flow_block1', skip_connection_type='conv', stride=2, depth_activation=False) x, skip1 = _xception_block(x, [256, 256, 256], 'entry_flow_block2', skip_connection_type='conv', stride=2, depth_activation=False, return_skip=True) x = _xception_block(x, [728, 728, 728], 'entry_flow_block3', skip_connection_type='conv', stride=entry_block3_stride, depth_activation=False) for i in range(16): x = _xception_block(x, [728, 728, 728], 'middle_flow_unit_{}'.format(i + 1), skip_connection_type='sum', stride=1, rate=middle_block_rate, depth_activation=False) x = _xception_block(x, [728, 1024, 1024], 'exit_flow_block1', skip_connection_type='conv', stride=1, rate=exit_block_rates[0], depth_activation=False) x = _xception_block(x, [1536, 1536, 2048], 'exit_flow_block2', skip_connection_type='none', stride=1, rate=exit_block_rates[1], depth_activation=True) else: OS = 8 first_block_filters = _make_divisible(32 * alpha, 8) x = Conv2D(first_block_filters, kernel_size=3, kernel_regularizer=regularizer, strides=(2, 2), padding='same', use_bias=False, name='Conv')(img_input) x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x) x = Activation(tf.nn.relu6, name='Conv_Relu6')(x) x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0, skip_connection=False) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1, skip_connection=False) x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2, skip_connection=True) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3, skip_connection=False) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4, skip_connection=True) x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5, skip_connection=True) # stride in block 6 changed from 2 -> 1, so we need to use rate = 2 x = _inverted_res_block( x, filters=64, alpha=alpha, stride=1, # 1! expansion=6, block_id=6, skip_connection=False) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2, expansion=6, block_id=7, skip_connection=True) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2, expansion=6, block_id=8, skip_connection=True) x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2, expansion=6, block_id=9, skip_connection=True) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2, expansion=6, block_id=10, skip_connection=False) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2, expansion=6, block_id=11, skip_connection=True) x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2, expansion=6, block_id=12, skip_connection=True) x = _inverted_res_block( x, filters=160, alpha=alpha, stride=1, rate=2, # 1! expansion=6, block_id=13, skip_connection=False) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4, expansion=6, block_id=14, skip_connection=True) x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4, expansion=6, block_id=15, skip_connection=True) x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, rate=4, expansion=6, block_id=16, skip_connection=False) # end of feature extractor # branching for Atrous Spatial Pyramid Pooling # Image Feature branch shape_before = tf.shape(x) b4 = GlobalAveragePooling2D()(x) # from (b_size, channels)->(b_size, 1, 1, channels) b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4) b4 = Conv2D(256, (1, 1), padding='same', kernel_regularizer=regularizer, use_bias=False, name='image_pooling')(b4) b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4) b4 = Activation(tf.nn.relu)(b4) # upsample. have to use compat because of the option align_corners size_before = tf.keras.backend.int_shape(x) size_of_b4 = tf.keras.backend.int_shape(b4) b4 = Lambda(lambda x: K.resize_images(x, size_before[1] / size_of_b4[1], size_before[2] / size_of_b4[2], data_format='channels_last', interpolation='bilinear'))(b4) # simple 1x1 b0 = Conv2D(256, (1, 1), padding='same', kernel_regularizer=regularizer, use_bias=False, name='aspp0')(x) b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0) b0 = Activation(tf.nn.relu, name='aspp0_activation')(b0) # there are only 2 branches in mobilenetV2. not sure why if backbone == 'xception': # rate = 6 (12) b1 = SepConv_BN(x, 256, 'aspp1', rate=atrous_rates[0], depth_activation=True, epsilon=1e-5) # rate = 12 (24) b2 = SepConv_BN(x, 256, 'aspp2', rate=atrous_rates[1], depth_activation=True, epsilon=1e-5) # rate = 18 (36) b3 = SepConv_BN(x, 256, 'aspp3', rate=atrous_rates[2], depth_activation=True, epsilon=1e-5) # concatenate ASPP branches & project x = Concatenate()([b4, b0, b1, b2, b3]) else: x = Concatenate()([b4, b0]) x = Conv2D(256, (1, 1), padding='same', kernel_regularizer=regularizer, use_bias=False, name='concat_projection')(x) x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x) x = Activation(tf.nn.relu)(x) x = Dropout(0.1)(x) # DeepLab v.3+ decoder if backbone == 'xception': # Feature projection # x4 (x2) block skip_size = tf.keras.backend.int_shape(skip1) size_before_x = tf.keras.backend.int_shape(x) x = Lambda(lambda xx: K.resize_images(xx, skip_size[1] / size_before_x[1], skip_size[2] / size_before_x[2], data_format='channels_last', interpolation='bilinear'))(x) dec_skip1 = Conv2D(48, (1, 1), padding='same', use_bias=False, name='feature_projection0')(skip1) dec_skip1 = BatchNormalization(name='feature_projection0_BN', epsilon=1e-5)(dec_skip1) dec_skip1 = Activation(tf.nn.relu)(dec_skip1) x = Concatenate()([x, dec_skip1]) x = SepConv_BN(x, 256, 'decoder_conv0', depth_activation=True, epsilon=1e-5) x = SepConv_BN(x, 256, 'decoder_conv1', depth_activation=True, epsilon=1e-5) # you can use it with arbitary number of num_classes if (weights == 'pascal_voc' and num_classes == 21) or (weights == 'cityscapes' and num_classes == 19): last_layer_name = 'logits_semantic' else: last_layer_name = 'custom_logits_semantic' x = Conv2D(num_classes, (1, 1), padding='same', name=last_layer_name)(x) size_before3 = tf.keras.backend.int_shape(img_input) size_of_last_x = tf.keras.backend.int_shape(x) x = Lambda(lambda xx: K.resize_images(xx, size_before3[1] / size_of_last_x[1], size_before3[2] / size_of_last_x[2], data_format='channels_last', interpolation='bilinear'))(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input if activation in {'softmax', 'sigmoid'}: x = Activation(activation)(x) model = Model(inputs, x, name='deeplabv3plus') # load weights if weights == 'pascal_voc' and load_weights_from_internet == True: if backbone == 'xception': weights_path = get_file( 'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_X, cache_subdir='models') else: weights_path = get_file( 'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH_MOBILE, cache_subdir='models') model.load_weights(weights_path, by_name=True) elif weights == 'cityscapes' and load_weights_from_internet == True: if backbone == 'xception': weights_path = get_file( 'deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5', WEIGHTS_PATH_X_CS, cache_subdir='models') else: weights_path = get_file( 'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5', WEIGHTS_PATH_MOBILE_CS, cache_subdir='models') model.load_weights(weights_path, by_name=True) return model
def SqueezeNet(include_top=True, weights="imagenet", input_tensor=None, input_shape=None, pooling=None, classes=1000): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = Convolution2D(64, kernel_size=(3, 3), strides=(2, 2), padding="same", activation="relu", name='conv1')(img_input) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='maxpool1', padding="valid")(x) x = _fire(x, (16, 64, 64), name="fire2") x = _fire(x, (16, 64, 64), name="fire3") x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='maxpool3', padding="valid")(x) x = _fire(x, (32, 128, 128), name="fire4") x = _fire(x, (32, 128, 128), name="fire5") x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='maxpool5', padding="valid")(x) x = _fire(x, (48, 192, 192), name="fire6") x = _fire(x, (48, 192, 192), name="fire7") x = _fire(x, (64, 256, 256), name="fire8") x = _fire(x, (64, 256, 256), name="fire9") if include_top: x = Dropout(0.5, name='dropout9')(x) x = Convolution2D(classes, (1, 1), padding='valid', name='conv10')(x) x = AveragePooling2D(pool_size=(13, 13), name='avgpool10')(x) x = Flatten(name='flatten10')(x) x = Activation("softmax", name='softmax')(x) else: if pooling == "avg": x = GlobalAveragePooling2D(name="avgpool10")(x) else: x = GlobalMaxPooling2D(name="maxpool10")(x) model = Model(img_input, x, name="squeezenet") if weights == 'imagenet': weights_path = get_file('squeezenet_weights.h5', WEIGHTS_PATH, cache_subdir='models') model.load_weights(weights_path) return model
def load_data( path="reuters.npz", num_words=None, skip_top=0, maxlen=None, test_split=0.2, seed=113, start_char=1, oov_char=2, index_from=3, **kwargs, ): """Loads the Reuters newswire classification dataset. This is a dataset of 11,228 newswires from Reuters, labeled over 46 topics. This was originally generated by parsing and preprocessing the classic Reuters-21578 dataset, but the preprocessing code is no longer packaged with Keras. See this [github discussion](https://github.com/keras-team/keras/issues/12072) for more info. Each newswire is encoded as a list of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words". As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word. Args: path: where to cache the data (relative to `~/.keras/dataset`). num_words: integer or None. Words are ranked by how often they occur (in the training set) and only the `num_words` most frequent words are kept. Any less frequent word will appear as `oov_char` value in the sequence data. If None, all words are kept. Defaults to None, so all words are kept. skip_top: skip the top N most frequently occurring words (which may not be informative). These words will appear as `oov_char` value in the dataset. Defaults to 0, so no words are skipped. maxlen: int or None. Maximum sequence length. Any longer sequence will be truncated. Defaults to None, which means no truncation. test_split: Float between 0 and 1. Fraction of the dataset to be used as test data. Defaults to 0.2, meaning 20% of the dataset is used as test data. seed: int. Seed for reproducible data shuffling. start_char: int. The start of a sequence will be marked with this character. Defaults to 1 because 0 is usually the padding character. oov_char: int. The out-of-vocabulary character. Words that were cut out because of the `num_words` or `skip_top` limits will be replaced with this character. index_from: int. Index actual words with this index and higher. **kwargs: Used for backwards compatibility. Returns: Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. **x_train, x_test**: lists of sequences, which are lists of indexes (integers). If the num_words argument was specific, the maximum possible index value is `num_words - 1`. If the `maxlen` argument was specified, the largest possible sequence length is `maxlen`. **y_train, y_test**: lists of integer labels (1 or 0). Note: The 'out of vocabulary' character is only used for words that were present in the training set but are not included because they're not making the `num_words` cut here. Words that were not seen in the training set but are in the test set have simply been skipped. """ # Legacy support if "nb_words" in kwargs: logging.warning("The `nb_words` argument in `load_data` " "has been renamed `num_words`.") num_words = kwargs.pop("nb_words") if kwargs: raise TypeError(f"Unrecognized keyword arguments: {str(kwargs)}") origin_folder = ( "https://storage.googleapis.com/tensorflow/tf-keras-datasets/") path = get_file( path, origin=origin_folder + "reuters.npz", file_hash= "d6586e694ee56d7a4e65172e12b3e987c03096cb01eab99753921ef915959916", ) with np.load(path, allow_pickle=True) as f: # pylint: disable=unexpected-keyword-arg xs, labels = f["x"], f["y"] rng = np.random.RandomState(seed) indices = np.arange(len(xs)) rng.shuffle(indices) xs = xs[indices] labels = labels[indices] if start_char is not None: xs = [[start_char] + [w + index_from for w in x] for x in xs] elif index_from: xs = [[w + index_from for w in x] for x in xs] if maxlen: xs, labels = _remove_long_seq(maxlen, xs, labels) if not num_words: num_words = max(max(x) for x in xs) # by convention, use 2 as OOV word # reserve 'index_from' (=3 by default) characters: # 0 (padding), 1 (start), 2 (OOV) if oov_char is not None: xs = [[w if skip_top <= w < num_words else oov_char for w in x] for x in xs] else: xs = [[w for w in x if skip_top <= w < num_words] for x in xs] idx = int(len(xs) * (1 - test_split)) x_train, y_train = np.array(xs[:idx], dtype="object"), np.array(labels[:idx]) x_test, y_test = np.array(xs[idx:], dtype="object"), np.array(labels[idx:]) return (x_train, y_train), (x_test, y_test)
def VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000, truncate=False): """Instantiates the VGG16 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the 3 fully-connected layers at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 244)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 48. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), include_top=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor import sys sys.path.append('../../') from utils.fxp import floating_to_fixed_tf # Block 1 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 4))(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 2))(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 2))(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 1))(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 0))(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 0))(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 0))(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 1))(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 1))(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 2))(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) # Block 5 x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 3))(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 4))(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 5))(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) if include_top: # Classification block x = Flatten(name='flatten')(x) x = Dense(4096, activation='relu', name='fc1')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 8))(x) x = Dense(4096, activation='relu', name='fc2')(x) if truncate: x = Lambda(lambda p: floating_to_fixed_tf(p, 16, 9))(x) x = Dense(classes, activation='softmax', name='predictions')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vgg16') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models') else: weights_path = get_file( 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='block5_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def SENET50(include_top=True, weights='vggface', input_tensor=None, input_shape=None, pooling=None, classes=8631): input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = Conv2D( 64, (7, 7), use_bias=False, strides=(2, 2), padding='same', name='conv1/7x7_s2')(img_input) x = BatchNormalization(axis=bn_axis, name='conv1/7x7_s2/bn')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = senet_conv_block(x, 3, [64, 64, 256], stage=2, block=1, strides=(1, 1)) x = senet_identity_block(x, 3, [64, 64, 256], stage=2, block=2) x = senet_identity_block(x, 3, [64, 64, 256], stage=2, block=3) x = senet_conv_block(x, 3, [128, 128, 512], stage=3, block=1) x = senet_identity_block(x, 3, [128, 128, 512], stage=3, block=2) x = senet_identity_block(x, 3, [128, 128, 512], stage=3, block=3) x = senet_identity_block(x, 3, [128, 128, 512], stage=3, block=4) x = senet_conv_block(x, 3, [256, 256, 1024], stage=4, block=1) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=2) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=3) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=4) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=5) x = senet_identity_block(x, 3, [256, 256, 1024], stage=4, block=6) x = senet_conv_block(x, 3, [512, 512, 2048], stage=5, block=1) x = senet_identity_block(x, 3, [512, 512, 2048], stage=5, block=2) x = senet_identity_block(x, 3, [512, 512, 2048], stage=5, block=3) x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='classifier')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='vggface_senet50') # load weights if weights == 'vggface': if include_top: weights_path = get_file('rcmalli_vggface_tf_senet50.h5', utils.SENET50_WEIGHTS_PATH, cache_subdir=utils.VGGFACE_DIR) else: weights_path = get_file('rcmalli_vggface_tf_notop_senet50.h5', utils.SENET50_WEIGHTS_PATH_NO_TOP, cache_subdir=utils.VGGFACE_DIR) model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='classifier') layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first') if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') elif weights is not None: model.load_weights(weights) return model
def MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000, classifier_activation='softmax', **kwargs): """Instantiates the MobileNet architecture. Reference: - [MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications]( https://arxiv.org/abs/1704.04861) This function returns a Keras image classification model, optionally loaded with weights pre-trained on ImageNet. For image classification use cases, see [this page for detailed examples]( https://keras.io/api/applications/#usage-examples-for-image-classification-models). For transfer learning use cases, make sure to read the [guide to transfer learning & fine-tuning]( https://keras.io/guides/transfer_learning/). Note: each Keras Application expects a specific kind of input preprocessing. For MobileNet, call `tf.keras.applications.mobilenet.preprocess_input` on your inputs before passing them to the model. `mobilenet.preprocess_input` will scale input pixels between -1 and 1. Args: input_shape: Optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or (3, 224, 224) (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. `(200, 200, 3)` would be one valid value. Default to `None`. `input_shape` will be ignored if the `input_tensor` is provided. alpha: Controls the width of the network. This is known as the width multiplier in the MobileNet paper. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. Default to 1.0. depth_multiplier: Depth multiplier for depthwise convolution. This is called the resolution multiplier in the MobileNet paper. Default to 1.0. dropout: Dropout rate. Default to 0.001. include_top: Boolean, whether to include the fully-connected layer at the top of the network. Default to `True`. weights: One of `None` (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded. Default to `imagenet`. input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. `input_tensor` is useful for sharing inputs between multiple different networks. Default to None. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` (default) means that the output of the model will be the 4D tensor output of the last convolutional block. - `avg` means that global average pooling will be applied to the output of the last convolutional block, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: Optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Defaults to 1000. classifier_activation: A `str` or callable. The activation function to use on the "top" layer. Ignored unless `include_top=True`. Set `classifier_activation=None` to return the logits of the "top" layer. When loading pretrained weights, `classifier_activation` can only be `None` or `"softmax"`. **kwargs: For backwards compatibility only. Returns: A `keras.Model` instance. """ global layers if 'layers' in kwargs: layers = kwargs.pop('layers') else: layers = VersionAwareLayers() if kwargs: raise ValueError(f'Unknown argument(s): {(kwargs,)}') if not (weights in {'imagenet', None} or tf.io.gfile.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded. ' f'Received weights={weights}') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top` ' 'as true, `classes` should be 1000. ' f'Received classes={classes}') # Determine proper input shape and default size. if input_shape is None: default_size = 224 else: if backend.image_data_format() == 'channels_first': rows = input_shape[1] cols = input_shape[2] else: rows = input_shape[0] cols = input_shape[1] if rows == cols and rows in [128, 160, 192, 224]: default_size = rows else: default_size = 224 input_shape = imagenet_utils.obtain_input_shape( input_shape, default_size=default_size, min_size=32, data_format=backend.image_data_format(), require_flatten=include_top, weights=weights) if backend.image_data_format() == 'channels_last': row_axis, col_axis = (0, 1) else: row_axis, col_axis = (1, 2) rows = input_shape[row_axis] cols = input_shape[col_axis] if weights == 'imagenet': if depth_multiplier != 1: raise ValueError('If imagenet weights are being loaded, ' 'depth multiplier must be 1. ' 'Received depth_multiplier={depth_multiplier}') if alpha not in [0.25, 0.50, 0.75, 1.0]: raise ValueError('If imagenet weights are being loaded, ' 'alpha can be one of' '`0.25`, `0.50`, `0.75` or `1.0` only. ' f'Received alpha={alpha}') if rows != cols or rows not in [128, 160, 192, 224]: rows = 224 logging.warning('`input_shape` is undefined or non-square, ' 'or `rows` is not in [128, 160, 192, 224]. ' 'Weights for input shape (224, 224) will be ' 'loaded as the default.') if input_tensor is None: img_input = layers.Input(shape=input_shape) else: if not backend.is_keras_tensor(input_tensor): img_input = layers.Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor x = _conv_block(img_input, 32, alpha, strides=(2, 2)) x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2) x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3) x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4) x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10) x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11) x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12) x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13) if include_top: x = layers.GlobalAveragePooling2D(keepdims=True)(x) x = layers.Dropout(dropout, name='dropout')(x) x = layers.Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x) x = layers.Reshape((classes, ), name='reshape_2')(x) imagenet_utils.validate_activation(classifier_activation, weights) x = layers.Activation(activation=classifier_activation, name='predictions')(x) else: if pooling == 'avg': x = layers.GlobalAveragePooling2D()(x) elif pooling == 'max': x = layers.GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = layer_utils.get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = training.Model(inputs, x, name='mobilenet_%0.2f_%s' % (alpha, rows)) # Load weights. if weights == 'imagenet': if alpha == 1.0: alpha_text = '1_0' elif alpha == 0.75: alpha_text = '7_5' elif alpha == 0.50: alpha_text = '5_0' else: alpha_text = '2_5' if include_top: model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows) weight_path = BASE_WEIGHT_PATH + model_name weights_path = data_utils.get_file(model_name, weight_path, cache_subdir='models') else: model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows) weight_path = BASE_WEIGHT_PATH + model_name weights_path = data_utils.get_file(model_name, weight_path, cache_subdir='models') model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
if args.model == "vgg19": x = Convolution2D(512, (3, 3), activation='relu', name='conv4_4', padding='same')(x) x = pooling_func(x) x = Convolution2D(512, (3, 3), activation='relu', name='conv5_1', padding='same')(x) x = Convolution2D(512, (3, 3), activation='relu', name='conv5_2', padding='same')(x) x = Convolution2D(512, (3, 3), activation='relu', name='conv5_3', padding='same')(x) if args.model == "vgg19": x = Convolution2D(512, (3, 3), activation='relu', name='conv5_4', padding='same')(x) x = pooling_func(x) model = Model(ip, x) if K.image_dim_ordering() == "th": if args.model == "vgg19": weights = get_file('vgg19_weights_th_dim_ordering_th_kernels_notop.h5', TH_19_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: weights = get_file('vgg16_weights_th_dim_ordering_th_kernels_notop.h5', THEANO_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: if args.model == "vgg19": weights = get_file('vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_19_WEIGHTS_PATH_NO_TOP, cache_subdir='models') else: weights = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models') model.load_weights(weights) if K.backend() == 'tensorflow' and K.image_dim_ordering() == "th": warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image dimension ordering convention ' '(`image_dim_ordering="th"`). '
def InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the Inception v3 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. Note that the default input image size for this model is 299x299. Arguments: include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(299, 299, 3)` (with `channels_last` data format) or `(3, 299, 299)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 139. E.g. `(150, 150, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. Returns: A Keras model instance. Raises: ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') input_1 = Input(shape = (299, 299, 3,), dtype = "float32") conv2d_1 = Conv2D(filters = 32, kernel_size = (3, 3), strides = (2, 2), padding = 'valid', use_bias = False)(input_1) batch_normalization_1 = BatchNormalization(name = 'batch_normalization_1', axis = 3, scale = False)(conv2d_1) activation_1 = Activation('relu')(batch_normalization_1) conv2d_2 = Conv2D(filters = 32, kernel_size = (3, 3), strides = (1, 1), padding = 'valid', use_bias = False)(activation_1) batch_normalization_2 = BatchNormalization(name = 'batch_normalization_2', axis = 3, scale = False)(conv2d_2) activation_2 = Activation('relu')(batch_normalization_2) conv2d_3 = Conv2D(filters = 64, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_2) batch_normalization_3 = BatchNormalization(name = 'batch_normalization_3', axis = 3, scale = False)(conv2d_3) activation_3 = Activation('relu')(batch_normalization_3) max_pooling2d_1 = MaxPooling2D(name = 'max_pooling2d_1', pool_size = (3, 3), strides = (2, 2), padding = 'valid')(activation_3) conv2d_4 = Conv2D(filters = 80, kernel_size = (1, 1), strides = (1, 1), padding = 'valid', use_bias = False)(max_pooling2d_1) batch_normalization_4 = BatchNormalization(name = 'batch_normalization_4', axis = 3, scale = False)(conv2d_4) activation_4 = Activation('relu')(batch_normalization_4) conv2d_5 = Conv2D(filters = 192, kernel_size = (3, 3), strides = (1, 1), padding = 'valid', use_bias = False)(activation_4) batch_normalization_5 = BatchNormalization(name = 'batch_normalization_5', axis = 3, scale = False)(conv2d_5) activation_5 = Activation('relu')(batch_normalization_5) max_pooling2d_2 = MaxPooling2D(name = 'max_pooling2d_2', pool_size = (3, 3), strides = (2, 2), padding = 'valid')(activation_5) conv2d_9 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(max_pooling2d_2) batch_normalization_9 = BatchNormalization(name = 'batch_normalization_9', axis = 3, scale = False)(conv2d_9) activation_9 = Activation('relu')(batch_normalization_9) conv2d_10 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_9) batch_normalization_10 = BatchNormalization(name = 'batch_normalization_10', axis = 3, scale = False)(conv2d_10) activation_10 = Activation('relu')(batch_normalization_10) conv2d_11 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_10) batch_normalization_11 = BatchNormalization(name = 'batch_normalization_11', axis = 3, scale = False)(conv2d_11) activation_11 = Activation('relu')(batch_normalization_11) conv2d_7 = Conv2D(filters = 48, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(max_pooling2d_2) batch_normalization_7 = BatchNormalization(name = 'batch_normalization_7', axis = 3, scale = False)(conv2d_7) activation_7 = Activation('relu')(batch_normalization_7) conv2d_8 = Conv2D(filters = 64, kernel_size = (5, 5), strides = (1, 1), padding = 'same', use_bias = False)(activation_7) batch_normalization_8 = BatchNormalization(name = 'batch_normalization_8', axis = 3, scale = False)(conv2d_8) activation_8 = Activation('relu')(batch_normalization_8) average_pooling2d_1 = AveragePooling2D(name = 'average_pooling2d_1', pool_size = (3, 3), strides = (1, 1), padding = 'same')(max_pooling2d_2) conv2d_12 = Conv2D(filters = 32, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_1) batch_normalization_12 = BatchNormalization(name = 'batch_normalization_12', axis = 3, scale = False)(conv2d_12) activation_12 = Activation('relu')(batch_normalization_12) conv2d_6 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(max_pooling2d_2) batch_normalization_6 = BatchNormalization(name = 'batch_normalization_6', axis = 3, scale = False)(conv2d_6) activation_6 = Activation('relu')(batch_normalization_6) mixed0 = layers.concatenate(name = 'mixed0', inputs = [activation_6, activation_8, activation_11, activation_12]) conv2d_16 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed0) batch_normalization_16 = BatchNormalization(name = 'batch_normalization_16', axis = 3, scale = False)(conv2d_16) activation_16 = Activation('relu')(batch_normalization_16) conv2d_17 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_16) batch_normalization_17 = BatchNormalization(name = 'batch_normalization_17', axis = 3, scale = False)(conv2d_17) activation_17 = Activation('relu')(batch_normalization_17) conv2d_18 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_17) batch_normalization_18 = BatchNormalization(name = 'batch_normalization_18', axis = 3, scale = False)(conv2d_18) activation_18 = Activation('relu')(batch_normalization_18) conv2d_14 = Conv2D(filters = 48, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed0) batch_normalization_14 = BatchNormalization(name = 'batch_normalization_14', axis = 3, scale = False)(conv2d_14) activation_14 = Activation('relu')(batch_normalization_14) conv2d_15 = Conv2D(filters = 64, kernel_size = (5, 5), strides = (1, 1), padding = 'same', use_bias = False)(activation_14) batch_normalization_15 = BatchNormalization(name = 'batch_normalization_15', axis = 3, scale = False)(conv2d_15) activation_15 = Activation('relu')(batch_normalization_15) average_pooling2d_2 = AveragePooling2D(name = 'average_pooling2d_2', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed0) conv2d_19 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_2) batch_normalization_19 = BatchNormalization(name = 'batch_normalization_19', axis = 3, scale = False)(conv2d_19) activation_19 = Activation('relu')(batch_normalization_19) conv2d_13 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed0) batch_normalization_13 = BatchNormalization(name = 'batch_normalization_13', axis = 3, scale = False)(conv2d_13) activation_13 = Activation('relu')(batch_normalization_13) mixed1 = layers.concatenate(name = 'mixed1', inputs = [activation_13, activation_15, activation_18, activation_19]) conv2d_23 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed1) batch_normalization_23 = BatchNormalization(name = 'batch_normalization_23', axis = 3, scale = False)(conv2d_23) activation_23 = Activation('relu')(batch_normalization_23) conv2d_24 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_23) batch_normalization_24 = BatchNormalization(name = 'batch_normalization_24', axis = 3, scale = False)(conv2d_24) activation_24 = Activation('relu')(batch_normalization_24) conv2d_25 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_24) batch_normalization_25 = BatchNormalization(name = 'batch_normalization_25', axis = 3, scale = False)(conv2d_25) activation_25 = Activation('relu')(batch_normalization_25) conv2d_21 = Conv2D(filters = 48, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed1) batch_normalization_21 = BatchNormalization(name = 'batch_normalization_21', axis = 3, scale = False)(conv2d_21) activation_21 = Activation('relu')(batch_normalization_21) conv2d_22 = Conv2D(filters = 64, kernel_size = (5, 5), strides = (1, 1), padding = 'same', use_bias = False)(activation_21) batch_normalization_22 = BatchNormalization(name = 'batch_normalization_22', axis = 3, scale = False)(conv2d_22) activation_22 = Activation('relu')(batch_normalization_22) average_pooling2d_3 = AveragePooling2D(name = 'average_pooling2d_3', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed1) conv2d_26 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_3) batch_normalization_26 = BatchNormalization(name = 'batch_normalization_26', axis = 3, scale = False)(conv2d_26) activation_26 = Activation('relu')(batch_normalization_26) conv2d_20 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed1) batch_normalization_20 = BatchNormalization(name = 'batch_normalization_20', axis = 3, scale = False)(conv2d_20) activation_20 = Activation('relu')(batch_normalization_20) mixed2 = layers.concatenate(name = 'mixed2', inputs = [activation_20, activation_22, activation_25, activation_26]) conv2d_28 = Conv2D(filters = 64, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed2) batch_normalization_28 = BatchNormalization(name = 'batch_normalization_28', axis = 3, scale = False)(conv2d_28) activation_28 = Activation('relu')(batch_normalization_28) conv2d_29 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_28) batch_normalization_29 = BatchNormalization(name = 'batch_normalization_29', axis = 3, scale = False)(conv2d_29) activation_29 = Activation('relu')(batch_normalization_29) conv2d_30 = Conv2D(filters = 96, kernel_size = (3, 3), strides = (2, 2), padding = 'valid', use_bias = False)(activation_29) batch_normalization_30 = BatchNormalization(name = 'batch_normalization_30', axis = 3, scale = False)(conv2d_30) activation_30 = Activation('relu')(batch_normalization_30) conv2d_27 = Conv2D(filters = 384, kernel_size = (3, 3), strides = (2, 2), padding = 'valid', use_bias = False)(mixed2) batch_normalization_27 = BatchNormalization(name = 'batch_normalization_27', axis = 3, scale = False)(conv2d_27) activation_27 = Activation('relu')(batch_normalization_27) max_pooling2d_3 = MaxPooling2D(name = 'max_pooling2d_3', pool_size = (3, 3), strides = (2, 2), padding = 'valid')(mixed2) mixed3 = layers.concatenate(name = 'mixed3', inputs = [activation_27, activation_30, max_pooling2d_3]) conv2d_35 = Conv2D(filters = 128, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed3) batch_normalization_35 = BatchNormalization(name = 'batch_normalization_35', axis = 3, scale = False)(conv2d_35) activation_35 = Activation('relu')(batch_normalization_35) conv2d_36 = Conv2D(filters = 128, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_35) batch_normalization_36 = BatchNormalization(name = 'batch_normalization_36', axis = 3, scale = False)(conv2d_36) activation_36 = Activation('relu')(batch_normalization_36) conv2d_37 = Conv2D(filters = 128, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_36) batch_normalization_37 = BatchNormalization(name = 'batch_normalization_37', axis = 3, scale = False)(conv2d_37) activation_37 = Activation('relu')(batch_normalization_37) conv2d_38 = Conv2D(filters = 128, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_37) batch_normalization_38 = BatchNormalization(name = 'batch_normalization_38', axis = 3, scale = False)(conv2d_38) activation_38 = Activation('relu')(batch_normalization_38) conv2d_39 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_38) batch_normalization_39 = BatchNormalization(name = 'batch_normalization_39', axis = 3, scale = False)(conv2d_39) activation_39 = Activation('relu')(batch_normalization_39) conv2d_32 = Conv2D(filters = 128, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed3) batch_normalization_32 = BatchNormalization(name = 'batch_normalization_32', axis = 3, scale = False)(conv2d_32) activation_32 = Activation('relu')(batch_normalization_32) conv2d_33 = Conv2D(filters = 128, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_32) batch_normalization_33 = BatchNormalization(name = 'batch_normalization_33', axis = 3, scale = False)(conv2d_33) activation_33 = Activation('relu')(batch_normalization_33) conv2d_34 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_33) batch_normalization_34 = BatchNormalization(name = 'batch_normalization_34', axis = 3, scale = False)(conv2d_34) activation_34 = Activation('relu')(batch_normalization_34) average_pooling2d_4 = AveragePooling2D(name = 'average_pooling2d_4', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed3) conv2d_40 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_4) batch_normalization_40 = BatchNormalization(name = 'batch_normalization_40', axis = 3, scale = False)(conv2d_40) activation_40 = Activation('relu')(batch_normalization_40) conv2d_31 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed3) batch_normalization_31 = BatchNormalization(name = 'batch_normalization_31', axis = 3, scale = False)(conv2d_31) activation_31 = Activation('relu')(batch_normalization_31) mixed4 = layers.concatenate(name = 'mixed4', inputs = [activation_31, activation_34, activation_39, activation_40]) conv2d_45 = Conv2D(filters = 160, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed4) batch_normalization_45 = BatchNormalization(name = 'batch_normalization_45', axis = 3, scale = False)(conv2d_45) activation_45 = Activation('relu')(batch_normalization_45) conv2d_46 = Conv2D(filters = 160, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_45) batch_normalization_46 = BatchNormalization(name = 'batch_normalization_46', axis = 3, scale = False)(conv2d_46) activation_46 = Activation('relu')(batch_normalization_46) conv2d_47 = Conv2D(filters = 160, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_46) batch_normalization_47 = BatchNormalization(name = 'batch_normalization_47', axis = 3, scale = False)(conv2d_47) activation_47 = Activation('relu')(batch_normalization_47) conv2d_48 = Conv2D(filters = 160, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_47) batch_normalization_48 = BatchNormalization(name = 'batch_normalization_48', axis = 3, scale = False)(conv2d_48) activation_48 = Activation('relu')(batch_normalization_48) conv2d_49 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_48) batch_normalization_49 = BatchNormalization(name = 'batch_normalization_49', axis = 3, scale = False)(conv2d_49) activation_49 = Activation('relu')(batch_normalization_49) conv2d_42 = Conv2D(filters = 160, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed4) batch_normalization_42 = BatchNormalization(name = 'batch_normalization_42', axis = 3, scale = False)(conv2d_42) activation_42 = Activation('relu')(batch_normalization_42) conv2d_43 = Conv2D(filters = 160, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_42) batch_normalization_43 = BatchNormalization(name = 'batch_normalization_43', axis = 3, scale = False)(conv2d_43) activation_43 = Activation('relu')(batch_normalization_43) conv2d_44 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_43) batch_normalization_44 = BatchNormalization(name = 'batch_normalization_44', axis = 3, scale = False)(conv2d_44) activation_44 = Activation('relu')(batch_normalization_44) average_pooling2d_5 = AveragePooling2D(name = 'average_pooling2d_5', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed4) conv2d_50 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_5) batch_normalization_50 = BatchNormalization(name = 'batch_normalization_50', axis = 3, scale = False)(conv2d_50) activation_50 = Activation('relu')(batch_normalization_50) conv2d_41 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed4) batch_normalization_41 = BatchNormalization(name = 'batch_normalization_41', axis = 3, scale = False)(conv2d_41) activation_41 = Activation('relu')(batch_normalization_41) mixed5 = layers.concatenate(name = 'mixed5', inputs = [activation_41, activation_44, activation_49, activation_50]) conv2d_55 = Conv2D(filters = 160, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed5) batch_normalization_55 = BatchNormalization(name = 'batch_normalization_55', axis = 3, scale = False)(conv2d_55) activation_55 = Activation('relu')(batch_normalization_55) conv2d_56 = Conv2D(filters = 160, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_55) batch_normalization_56 = BatchNormalization(name = 'batch_normalization_56', axis = 3, scale = False)(conv2d_56) activation_56 = Activation('relu')(batch_normalization_56) conv2d_57 = Conv2D(filters = 160, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_56) batch_normalization_57 = BatchNormalization(name = 'batch_normalization_57', axis = 3, scale = False)(conv2d_57) activation_57 = Activation('relu')(batch_normalization_57) conv2d_58 = Conv2D(filters = 160, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_57) batch_normalization_58 = BatchNormalization(name = 'batch_normalization_58', axis = 3, scale = False)(conv2d_58) activation_58 = Activation('relu')(batch_normalization_58) conv2d_59 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_58) batch_normalization_59 = BatchNormalization(name = 'batch_normalization_59', axis = 3, scale = False)(conv2d_59) activation_59 = Activation('relu')(batch_normalization_59) conv2d_52 = Conv2D(filters = 160, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed5) batch_normalization_52 = BatchNormalization(name = 'batch_normalization_52', axis = 3, scale = False)(conv2d_52) activation_52 = Activation('relu')(batch_normalization_52) conv2d_53 = Conv2D(filters = 160, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_52) batch_normalization_53 = BatchNormalization(name = 'batch_normalization_53', axis = 3, scale = False)(conv2d_53) activation_53 = Activation('relu')(batch_normalization_53) conv2d_54 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_53) batch_normalization_54 = BatchNormalization(name = 'batch_normalization_54', axis = 3, scale = False)(conv2d_54) activation_54 = Activation('relu')(batch_normalization_54) average_pooling2d_6 = AveragePooling2D(name = 'average_pooling2d_6', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed5) conv2d_60 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_6) batch_normalization_60 = BatchNormalization(name = 'batch_normalization_60', axis = 3, scale = False)(conv2d_60) activation_60 = Activation('relu')(batch_normalization_60) conv2d_51 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed5) batch_normalization_51 = BatchNormalization(name = 'batch_normalization_51', axis = 3, scale = False)(conv2d_51) activation_51 = Activation('relu')(batch_normalization_51) mixed6 = layers.concatenate(name = 'mixed6', inputs = [activation_51, activation_54, activation_59, activation_60]) conv2d_65 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed6) batch_normalization_65 = BatchNormalization(name = 'batch_normalization_65', axis = 3, scale = False)(conv2d_65) activation_65 = Activation('relu')(batch_normalization_65) conv2d_66 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_65) batch_normalization_66 = BatchNormalization(name = 'batch_normalization_66', axis = 3, scale = False)(conv2d_66) activation_66 = Activation('relu')(batch_normalization_66) conv2d_67 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_66) batch_normalization_67 = BatchNormalization(name = 'batch_normalization_67', axis = 3, scale = False)(conv2d_67) activation_67 = Activation('relu')(batch_normalization_67) conv2d_68 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_67) batch_normalization_68 = BatchNormalization(name = 'batch_normalization_68', axis = 3, scale = False)(conv2d_68) activation_68 = Activation('relu')(batch_normalization_68) conv2d_69 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_68) batch_normalization_69 = BatchNormalization(name = 'batch_normalization_69', axis = 3, scale = False)(conv2d_69) activation_69 = Activation('relu')(batch_normalization_69) conv2d_62 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed6) batch_normalization_62 = BatchNormalization(name = 'batch_normalization_62', axis = 3, scale = False)(conv2d_62) activation_62 = Activation('relu')(batch_normalization_62) conv2d_63 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_62) batch_normalization_63 = BatchNormalization(name = 'batch_normalization_63', axis = 3, scale = False)(conv2d_63) activation_63 = Activation('relu')(batch_normalization_63) conv2d_64 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_63) batch_normalization_64 = BatchNormalization(name = 'batch_normalization_64', axis = 3, scale = False)(conv2d_64) activation_64 = Activation('relu')(batch_normalization_64) average_pooling2d_7 = AveragePooling2D(name = 'average_pooling2d_7', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed6) conv2d_70 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_7) batch_normalization_70 = BatchNormalization(name = 'batch_normalization_70', axis = 3, scale = False)(conv2d_70) activation_70 = Activation('relu')(batch_normalization_70) conv2d_61 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed6) batch_normalization_61 = BatchNormalization(name = 'batch_normalization_61', axis = 3, scale = False)(conv2d_61) activation_61 = Activation('relu')(batch_normalization_61) mixed7 = layers.concatenate(name = 'mixed7', inputs = [activation_61, activation_64, activation_69, activation_70]) conv2d_73 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed7) batch_normalization_73 = BatchNormalization(name = 'batch_normalization_73', axis = 3, scale = False)(conv2d_73) activation_73 = Activation('relu')(batch_normalization_73) conv2d_74 = Conv2D(filters = 192, kernel_size = (1, 7), strides = (1, 1), padding = 'same', use_bias = False)(activation_73) batch_normalization_74 = BatchNormalization(name = 'batch_normalization_74', axis = 3, scale = False)(conv2d_74) activation_74 = Activation('relu')(batch_normalization_74) conv2d_75 = Conv2D(filters = 192, kernel_size = (7, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_74) batch_normalization_75 = BatchNormalization(name = 'batch_normalization_75', axis = 3, scale = False)(conv2d_75) activation_75 = Activation('relu')(batch_normalization_75) conv2d_76 = Conv2D(filters = 192, kernel_size = (3, 3), strides = (2, 2), padding = 'valid', use_bias = False)(activation_75) batch_normalization_76 = BatchNormalization(name = 'batch_normalization_76', axis = 3, scale = False)(conv2d_76) activation_76 = Activation('relu')(batch_normalization_76) conv2d_71 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed7) batch_normalization_71 = BatchNormalization(name = 'batch_normalization_71', axis = 3, scale = False)(conv2d_71) activation_71 = Activation('relu')(batch_normalization_71) conv2d_72 = Conv2D(filters = 320, kernel_size = (3, 3), strides = (2, 2), padding = 'valid', use_bias = False)(activation_71) batch_normalization_72 = BatchNormalization(name = 'batch_normalization_72', axis = 3, scale = False)(conv2d_72) activation_72 = Activation('relu')(batch_normalization_72) max_pooling2d_4 = MaxPooling2D(name = 'max_pooling2d_4', pool_size = (3, 3), strides = (2, 2), padding = 'valid')(mixed7) mixed8 = layers.concatenate(name = 'mixed8', inputs = [activation_72, activation_76, max_pooling2d_4]) conv2d_81 = Conv2D(filters = 448, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed8) batch_normalization_81 = BatchNormalization(name = 'batch_normalization_81', axis = 3, scale = False)(conv2d_81) activation_81 = Activation('relu')(batch_normalization_81) conv2d_82 = Conv2D(filters = 384, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_81) batch_normalization_82 = BatchNormalization(name = 'batch_normalization_82', axis = 3, scale = False)(conv2d_82) activation_82 = Activation('relu')(batch_normalization_82) conv2d_83 = Conv2D(filters = 384, kernel_size = (1, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_82) batch_normalization_83 = BatchNormalization(name = 'batch_normalization_83', axis = 3, scale = False)(conv2d_83) activation_83 = Activation('relu')(batch_normalization_83) conv2d_84 = Conv2D(filters = 384, kernel_size = (3, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_82) batch_normalization_84 = BatchNormalization(name = 'batch_normalization_84', axis = 3, scale = False)(conv2d_84) activation_84 = Activation('relu')(batch_normalization_84) concatenate_1 = layers.concatenate(name = 'concatenate_1', inputs = [activation_83, activation_84]) conv2d_78 = Conv2D(filters = 384, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed8) batch_normalization_78 = BatchNormalization(name = 'batch_normalization_78', axis = 3, scale = False)(conv2d_78) activation_78 = Activation('relu')(batch_normalization_78) conv2d_79 = Conv2D(filters = 384, kernel_size = (1, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_78) batch_normalization_79 = BatchNormalization(name = 'batch_normalization_79', axis = 3, scale = False)(conv2d_79) activation_79 = Activation('relu')(batch_normalization_79) conv2d_80 = Conv2D(filters = 384, kernel_size = (3, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_78) batch_normalization_80 = BatchNormalization(name = 'batch_normalization_80', axis = 3, scale = False)(conv2d_80) activation_80 = Activation('relu')(batch_normalization_80) mixed9_0 = layers.concatenate(name = 'mixed9_0', inputs = [activation_79, activation_80]) average_pooling2d_8 = AveragePooling2D(name = 'average_pooling2d_8', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed8) conv2d_85 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_8) batch_normalization_85 = BatchNormalization(name = 'batch_normalization_85', axis = 3, scale = False)(conv2d_85) activation_85 = Activation('relu')(batch_normalization_85) conv2d_77 = Conv2D(filters = 320, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed8) batch_normalization_77 = BatchNormalization(name = 'batch_normalization_77', axis = 3, scale = False)(conv2d_77) activation_77 = Activation('relu')(batch_normalization_77) mixed9 = layers.concatenate(name = 'mixed9', inputs = [activation_77, mixed9_0, concatenate_1, activation_85]) conv2d_90 = Conv2D(filters = 448, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed9) batch_normalization_90 = BatchNormalization(name = 'batch_normalization_90', axis = 3, scale = False)(conv2d_90) activation_90 = Activation('relu')(batch_normalization_90) conv2d_91 = Conv2D(filters = 384, kernel_size = (3, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_90) batch_normalization_91 = BatchNormalization(name = 'batch_normalization_91', axis = 3, scale = False)(conv2d_91) activation_91 = Activation('relu')(batch_normalization_91) conv2d_92 = Conv2D(filters = 384, kernel_size = (1, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_91) batch_normalization_92 = BatchNormalization(name = 'batch_normalization_92', axis = 3, scale = False)(conv2d_92) activation_92 = Activation('relu')(batch_normalization_92) conv2d_93 = Conv2D(filters = 384, kernel_size = (3, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_91) batch_normalization_93 = BatchNormalization(name = 'batch_normalization_93', axis = 3, scale = False)(conv2d_93) activation_93 = Activation('relu')(batch_normalization_93) concatenate_2 = layers.concatenate(name = 'concatenate_2', inputs = [activation_92, activation_93]) conv2d_87 = Conv2D(filters = 384, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed9) batch_normalization_87 = BatchNormalization(name = 'batch_normalization_87', axis = 3, scale = False)(conv2d_87) activation_87 = Activation('relu')(batch_normalization_87) conv2d_88 = Conv2D(filters = 384, kernel_size = (1, 3), strides = (1, 1), padding = 'same', use_bias = False)(activation_87) batch_normalization_88 = BatchNormalization(name = 'batch_normalization_88', axis = 3, scale = False)(conv2d_88) activation_88 = Activation('relu')(batch_normalization_88) conv2d_89 = Conv2D(filters = 384, kernel_size = (3, 1), strides = (1, 1), padding = 'same', use_bias = False)(activation_87) batch_normalization_89 = BatchNormalization(name = 'batch_normalization_89', axis = 3, scale = False)(conv2d_89) activation_89 = Activation('relu')(batch_normalization_89) mixed9_1 = layers.concatenate(name = 'mixed9_1', inputs = [activation_88, activation_89]) average_pooling2d_9 = AveragePooling2D(name = 'average_pooling2d_9', pool_size = (3, 3), strides = (1, 1), padding = 'same')(mixed9) conv2d_94 = Conv2D(filters = 192, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(average_pooling2d_9) batch_normalization_94 = BatchNormalization(name = 'batch_normalization_94', axis = 3, scale = False)(conv2d_94) activation_94 = Activation('relu')(batch_normalization_94) conv2d_86 = Conv2D(filters = 320, kernel_size = (1, 1), strides = (1, 1), padding = 'same', use_bias = False)(mixed9) batch_normalization_86 = BatchNormalization(name = 'batch_normalization_86', axis = 3, scale = False)(conv2d_86) activation_86 = Activation('relu')(batch_normalization_86) mixed10 = layers.concatenate(name = 'mixed10', inputs = [activation_86, mixed9_1, concatenate_2, activation_94]) avg_pool = GlobalAveragePooling2D()(mixed10) predictions = Dense(units = 1000, use_bias = True)(avg_pool) predictions_activation = Activation('softmax')(predictions) model = Model(inputs = [input_1], outputs = [predictions_activation]) # load weights if weights == 'imagenet': if K.image_data_format() == 'channels_first': if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') if include_top: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='9a0d58056eeedaa3f26cb7ebd46da564') else: weights_path = get_file( 'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='bcbd6486424b2319ff4ef7d526e38f63') model.load_weights(weights_path) if K.backend() == 'theano': convert_all_kernels_in_model(model) return model
def EfficientNet(width_coefficient, depth_coefficient, default_size, dropout_rate=0.2, drop_connect_rate=0.2, depth_divisor=8, activation_fn=swish, blocks_args=DEFAULT_BLOCKS_ARGS, model_name='efficientnet', include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, num_classes=1000, **kwargs): # Instantiates the EfficientNet architecture using given scaling coefficients. """ # Arguments width_coefficient: float, scaling coefficient for network width. depth_coefficient: float, scaling coefficient for network depth. default_size: integer, default input image size. dropout_rate: float, dropout rate before final classifier layer. drop_connect_rate: float, dropout rate at skip connections. depth_divisor: integer, a unit of network width. activation_fn: activation function. blocks_args: list of dicts, parameters to construct block modules. model_name: string, model name. include_top: whether to include the FC layer at the top of the network. weights: `None` (random initialization), 'imagenet' or the path to any weights. input_tensor: optional Keras tensor (output of `layers.Input()`) input_shape: tuple, only to be specified if `include_top` is False. pooling: Optional mode for feature extraction when `include_top` is `False`. - `None`: the output of model is the 4D tensor of the last conv layer - `avg` means global average pooling and the output as a 2D tensor. - `max` means global max pooling will be applied. num_classes: specified if `include_top` is True # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights` or invalid input shape. """ if not (weights in {'imagenet', None} or os.path.exists(weights)): raise ValueError('The `weights` argument should be either ' '`None` (random initialization), `imagenet` ' '(pre-training on ImageNet), ' 'or the path to the weights file to be loaded.') if weights == 'imagenet' and include_top and num_classes != 1000: raise ValueError( 'If using `weights` as `"imagenet"` with `include_top`' ' as true, `classes` should be 1000') # Determine the proper input shape input_shape = _obtain_input_shape(input_shape, default_size=default_size, min_size=32, data_format=K.image_data_format(), require_flatten=include_top, weights=weights) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor bn_axis = -1 if K.image_data_format() == 'channels_last' else 1 def round_filters(filters, divisor=depth_divisor): # Round number of filters based on depth multiplier. filters *= width_coefficient new_filters = max(divisor, int(filters + divisor / 2) // divisor * divisor) # Ensure the round-down does not go down by more than 10%. if new_filters < 0.9 * filters: new_filters += divisor return int(new_filters) def round_repeats(repeats): # Round number of repeats based on depth multiplier. return int(math.ceil(depth_coefficient * repeats)) # Build the stem x = img_input x = ZeroPadding2D(padding=correct_pad(K, x, 3), name='stem_conv_pad')(x) x = Conv2D(round_filters(32), kernel_size=(3, 3), strides=2, padding='valid', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='stem_conv')(x) x = BatchNormalization(axis=bn_axis, name='stem_bn')(x) x = Activation(activation_fn, name='stem_activation')(x) # Build the blocks from copy import deepcopy blocks_args = deepcopy(blocks_args) # See the above blocks_args b = 0 blocks = float(sum(args['repeats'] for args in blocks_args)) for (i, args) in enumerate(blocks_args): assert args['repeats'] > 0 # Update the block input and output filters based on depth multiplier. args['filters_in'] = round_filters(args['filters_in']) args['filters_out'] = round_filters(args['filters_out']) for j in range(round_repeats(args.pop('repeats'))): # The first block needs to take care of stride and filter size growth. if j > 0: args['strides'] = 1 args['filters_in'] = args['filters_out'] x = block(x, activation_fn, drop_connect_rate * b / blocks, name='block{}{}_'.format(i + 1, chr(j + 97)), **args) b += 1 # Build the top x = Conv2D(round_filters(1280), kernel_size=(1, 1), padding='same', use_bias=False, kernel_initializer=CONV_KERNEL_INITIALIZER, name='top_conv')(x) x = BatchNormalization(axis=bn_axis, name='top_bn')(x) x = Activation(activation_fn, name='top_activation')(x) if include_top: x = GlobalAveragePooling2D(name='avg_pool')(x) if dropout_rate > 0: x = Dropout(dropout_rate, name='top_dropout')(x) x = Dense(num_classes, activation='softmax', kernel_initializer=DENSE_KERNEL_INITIALIZER, name='probs')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D(name='avg_pool')(x) elif pooling == 'max': x = GlobalMaxPooling2D(name='max_pool')(x) # Ensure the model considers any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Build the model. model = Model(inputs, x, name=model_name) # Load weights. if weights == 'imagenet': if include_top: file_suff = '_weights_tf_dim_ordering_tf_kernels_autoaugment.h5' file_hash = WEIGHTS_HASHES[model_name[-2:]][0] else: file_suff = '_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5' file_hash = WEIGHTS_HASHES[model_name[-2:]][1] file_name = model_name + file_suff weights_path = get_file(file_name, BASE_WEIGHTS_PATH + file_name, cache_subdir='models', file_hash=file_hash) model.load_weights(weights_path) elif weights is not None: model.load_weights(weights) return model
classifier = nn.classifier(shared_layers, roi_input, C.num_rois, nb_classes=len(classes_count), trainable=True) model_rpn = Model(img_input, rpn[:2]) model_classifier = Model([img_input, roi_input], classifier) # this is a model that holds both the RPN and the classifier, used to load/save weights for the models model_all = Model([img_input, roi_input], rpn[:2] + classifier) # download weights if there is not available KERAS_MODEL_BASEURL = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/' weights_path = get_file(C.base_net_weights, KERAS_MODEL_BASEURL + C.base_net_weights, cache_subdir='models') try: print('loading weights {}'.format(C.base_net_weights)) model_rpn.load_weights(weights_path, by_name=True) model_classifier.load_weights(weights_path, by_name=True) except: print( 'Could not load pretrained model weights. Weights can be found in the keras application folder \ https://github.com/fchollet/keras/tree/master/keras/applications') optimizer = Adam(lr=1e-5) optimizer_classifier = Adam(lr=1e-5) model_rpn.compile( optimizer=optimizer,
def C3D(weights='sports1M'): if weights not in {'sports1M', None}: raise ValueError('weights should be either be sports1M or None') if K.image_data_format() == 'channels_last': shape = (16, 112, 112, 3) else: shape = (3, 16, 112, 112) model = Sequential() model.add( Conv3D(64, 3, activation='relu', padding='same', name='conv1', input_shape=shape)) model.add( MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), padding='same', name='pool1')) model.add(Conv3D(128, 3, activation='relu', padding='same', name='conv2')) model.add( MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='valid', name='pool2')) model.add(Conv3D(256, 3, activation='relu', padding='same', name='conv3a')) model.add(Conv3D(256, 3, activation='relu', padding='same', name='conv3b')) model.add( MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='valid', name='pool3')) model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv4a')) model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv4b')) model.add( MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='valid', name='pool4')) model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv5a')) model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv5b')) model.add(ZeroPadding3D(padding=(0, 1, 1))) model.add( MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='valid', name='pool5')) model.add(Flatten()) model.add(Dense(4096, activation='relu', name='fc6')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu', name='fc7')) model.add(Dropout(0.5)) model.add(Dense(487, activation='softmax', name='fc8')) if weights == 'sports1M': weights_path = get_file('sports1M_weights_tf.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='b7a93b2f9156ccbebe3ca24b41fc5402') model.load_weights(weights_path) return model
y = np.zeros(len(word_idx) + 1) # let's not forget that index 0 is reserved y[word_idx[answer]] = 1 X.append(x) Xq.append(xq) Y.append(y) return pad_sequences(X, maxlen=story_maxlen), pad_sequences(Xq, maxlen=query_maxlen), np.array(Y) RNN = recurrent.LSTM EMBED_HIDDEN_SIZE = 50 SENT_HIDDEN_SIZE = 100 QUERY_HIDDEN_SIZE = 100 BATCH_SIZE = 32 EPOCHS = 40 print('RNN / Embed / Sent / Query = {}, {}, {}, {}'.format(RNN, EMBED_HIDDEN_SIZE, SENT_HIDDEN_SIZE, QUERY_HIDDEN_SIZE)) path = get_file('babi-tasks-v1-2.tar.gz', origin='http://www.thespermwhale.com/jaseweston/babi/tasks_1-20_v1-2.tar.gz') tar = tarfile.open(path) # Default QA1 with 1000 samples # challenge = 'tasks_1-20_v1-2/en/qa1_single-supporting-fact_{}.txt' # QA1 with 10,000 samples # challenge = 'tasks_1-20_v1-2/en-10k/qa1_single-supporting-fact_{}.txt' # QA2 with 1000 samples challenge = 'tasks_1-20_v1-2/en/qa2_two-supporting-facts_{}.txt' # QA2 with 10,000 samples # challenge = 'tasks_1-20_v1-2/en-10k/qa2_two-supporting-facts_{}.txt' train = get_stories(tar.extractfile(challenge.format('train'))) test = get_stories(tar.extractfile(challenge.format('test'))) vocab = sorted(reduce(lambda x, y: x | y, (set(story + q + [answer]) for story, q, answer in train + test))) # Reserve 0 for masking via pad_sequences vocab_size = len(vocab) + 1
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): """Instantiates the ResNet50 architecture. Optionally loads weights pre-trained on ImageNet. Note that when using TensorFlow, for best performance you should set `image_data_format="channels_last"` in your Keras config at ~/.keras/keras.json. The model and the weights are compatible with both TensorFlow and Theano. The data format convention used by the model is the one specified in your Keras config file. # Arguments include_top: whether to include the fully-connected layer at the top of the network. weights: one of `None` (random initialization) or "imagenet" (pre-training on ImageNet). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. input_shape: optional shape tuple, only to be specified if `include_top` is False (otherwise the input shape has to be `(224, 224, 3)` (with `channels_last` data format) or `(3, 224, 244)` (with `channels_first` data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 197. E.g. `(200, 200, 3)` would be one valid value. pooling: Optional pooling mode for feature extraction when `include_top` is `False`. - `None` means that the output of the model will be the 4D tensor output of the last convolutional layer. - `avg` means that global average pooling will be applied to the output of the last convolutional layer, and thus the output of the model will be a 2D tensor. - `max` means that global max pooling will be applied. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. # Raises ValueError: in case of invalid argument for `weights`, or invalid input shape. """ if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pre-training on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') # Determine proper input shape input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=197, data_format=K.image_data_format(), require_flatten=include_top) if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = ZeroPadding2D((3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') x = AveragePooling2D((7, 7), name='avg_pool')(x) if include_top: x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) else: if pooling == 'avg': x = GlobalAveragePooling2D()(x) elif pooling == 'max': x = GlobalMaxPooling2D()(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file( 'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path) if K.backend() == 'theano': layer_utils.convert_all_kernels_in_model(model) if K.image_data_format() == 'channels_first': if include_top: maxpool = model.get_layer(name='avg_pool') shape = maxpool.output_shape[1:] dense = model.get_layer(name='fc1000') layer_utils.convert_dense_weights_data_format( dense, shape, 'channels_first') if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image data format convention ' '(`image_data_format="channels_first"`). ' 'For best performance, set ' '`image_data_format="channels_last"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') return model
def download_file(url, filename): local_path = get_file(filename, origin=url) return local_path