我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用keras.layers.InputLayer()。
def train(train_generator,train_size,input_num,dims_num): print("Start Train Job! ") start=time.time() inputs=InputLayer(input_shape=(input_num,dims_num),batch_size=batch_size) layer1=Dense(100,activation="relu") layer2=Dense(20,activation="relu") flatten=Flatten() layer3=Dense(2,activation="softmax",name="Output") optimizer=Adam() model=Sequential() model.add(inputs) model.add(layer1) model.add(Dropout(0.5)) model.add(layer2) model.add(Dropout(0.5)) model.add(flatten) model.add(layer3) call=TensorBoard(log_dir=log_dir,write_grads=True,histogram_freq=1) model.compile(optimizer,loss="categorical_crossentropy",metrics=["accuracy"]) model.fit_generator(train_generator,steps_per_epoch=train_size//batch_size,epochs=epochs_num,callbacks=[call]) # model.fit_generator(train_generator, steps_per_epoch=5, epochs=5, callbacks=[call]) model.save(model_dir) end=time.time() print("Over train job in %f s"%(end-start))
def train(train_generator,train_size,input_num,dims_num): print("Start Train Job! ") start=time.time() inputs=InputLayer(input_shape=(input_num,dims_num),batch_size=batch_size) layer1=LSTM(128) output=Dense(2,activation="softmax",name="Output") optimizer=Adam() model=Sequential() model.add(inputs) model.add(layer1) model.add(Dropout(0.5)) model.add(output) call=TensorBoard(log_dir=log_dir,write_grads=True,histogram_freq=1) model.compile(optimizer,loss="categorical_crossentropy",metrics=["accuracy"]) model.fit_generator(train_generator,steps_per_epoch=train_size//batch_size,epochs=epochs_num,callbacks=[call]) # model.fit_generator(train_generator, steps_per_epoch=5, epochs=5, callbacks=[call]) model.save(model_dir) end=time.time() print("Over train job in %f s"%(end-start))
def fsrcnn(x, d=56, s=12, m=4, scale=3): """Build an FSRCNN model. See https://arxiv.org/abs/1608.00367 """ model = Sequential() model.add(InputLayer(input_shape=x.shape[-3:])) c = x.shape[-1] f = [5, 1] + [3] * m + [1] n = [d, s] + [s] * m + [d] for ni, fi in zip(n, f): model.add(Conv2D(ni, fi, padding='same', kernel_initializer='he_normal', activation='relu')) model.add(Conv2DTranspose(c, 9, strides=scale, padding='same', kernel_initializer='he_normal')) return model
def nsfsrcnn(x, d=56, s=12, m=4, scale=3, pos=1): """Build an FSRCNN model, but change deconv position. See https://arxiv.org/abs/1608.00367 """ model = Sequential() model.add(InputLayer(input_shape=x.shape[-3:])) c = x.shape[-1] f1 = [5, 1] + [3] * pos n1 = [d, s] + [s] * pos f2 = [3] * (m - pos - 1) + [1] n2 = [s] * (m - pos - 1) + [d] f3 = 9 n3 = c for ni, fi in zip(n1, f1): model.add(Conv2D(ni, fi, padding='same', kernel_initializer='he_normal', activation='relu')) model.add(Conv2DTranspose(s, 3, strides=scale, padding='same', kernel_initializer='he_normal')) for ni, fi in zip(n2, f2): model.add(Conv2D(ni, fi, padding='same', kernel_initializer='he_normal', activation='relu')) model.add(Conv2D(n3, f3, padding='same', kernel_initializer='he_normal')) return model
def espcn(x, f=[5, 3, 3], n=[64, 32], scale=3): """Build an ESPCN model. See https://arxiv.org/abs/1609.05158 """ assert len(f) == len(n) + 1 model = Sequential() model.add(InputLayer(input_shape=x.shape[1:])) c = x.shape[-1] for ni, fi in zip(n, f): model.add(Conv2D(ni, fi, padding='same', kernel_initializer='he_normal', activation='tanh')) model.add(Conv2D(c * scale ** 2, f[-1], padding='same', kernel_initializer='he_normal')) model.add(Conv2DSubPixel(scale)) return model
def make_model_full(inshape, num_classes, weights_file=None): model = Sequential() model.add(KL.InputLayer(input_shape=inshape[1:])) # model.add(KL.Conv2D(32, (3, 3), padding='same', input_shape=inshape[1:])) model.add(KL.Conv2D(32, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Conv2D(32, (3, 3))) model.add(KL.Activation('relu')) model.add(KL.MaxPooling2D(pool_size=(2, 2))) model.add(KL.Dropout(0.25)) model.add(KL.Conv2D(64, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Conv2D(64, (3, 3))) model.add(KL.Activation('relu')) model.add(KL.MaxPooling2D(pool_size=(2, 2))) model.add(KL.Dropout(0.25)) model.add(KL.Flatten()) model.add(KL.Dense(512)) model.add(KL.Activation('relu')) model.add(KL.Dropout(0.5)) model.add(KL.Dense(num_classes)) model.add(KL.Activation('softmax')) if weights_file is not None and os.path.exists(weights_file): model.load_weights(weights_file) return model
def make_model_small(inshape, num_classes, weights_file=None): '''Small model for debugging device placements.''' model = Sequential() model.add(KL.InputLayer(input_shape=inshape[1:])) model.add(KL.Conv2D(32, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Flatten()) # model.add(Dropout(0.5)) model.add(KL.Dense(num_classes)) model.add(KL.Activation('softmax')) if weights_file is not None and os.path.exists(weights_file): model.load_weights(weights_file) return model
def make_model(train_input, num_classes, weights_file=None): ''' :param train_input: Either tensorflow Tensor or tuple/list shape. Bad style since the parameter can be of different types, but seems Ok here. :type train_input: tf.Tensor or tuple/list ''' model = Sequential() # model.add(KL.InputLayer(input_shape=inshape[1:])) if isinstance(train_input, tf.Tensor): model.add(KL.InputLayer(input_tensor=train_input)) else: model.add(KL.InputLayer(input_shape=train_input)) model.add(KL.Conv2D(32, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Conv2D(32, (3, 3))) model.add(KL.Activation('relu')) model.add(KL.MaxPooling2D(pool_size=(2, 2))) model.add(KL.Dropout(0.25)) model.add(KL.Conv2D(64, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Conv2D(64, (3, 3))) model.add(KL.Activation('relu')) model.add(KL.MaxPooling2D(pool_size=(2, 2))) model.add(KL.Dropout(0.25)) model.add(KL.Flatten()) model.add(KL.Dense(512)) model.add(KL.Activation('relu')) model.add(KL.Dropout(0.5)) model.add(KL.Dense(num_classes)) model.add(KL.Activation('softmax')) if weights_file is not None and os.path.exists(weights_file): model.load_weights(weights_file) return model
def make_model_small(inshape, num_classes, weights_file=None): model = Sequential() model.add(KL.InputLayer(input_shape=inshape[1:])) model.add(KL.Conv2D(32, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Flatten()) # model.add(Dropout(0.5)) model.add(KL.Dense(num_classes)) model.add(KL.Activation('softmax')) if weights_file is not None and os.path.exists(weights_file): model.load_weights(weights_file) return model
def make_model(inshape, num_classes, weights_file=None): model = Sequential() model.add(KL.InputLayer(input_shape=inshape[1:])) # model.add(KL.Conv2D(32, (3, 3), padding='same', input_shape=inshape[1:])) model.add(KL.Conv2D(32, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Conv2D(32, (3, 3))) model.add(KL.Activation('relu')) model.add(KL.MaxPooling2D(pool_size=(2, 2))) model.add(KL.Dropout(0.25)) model.add(KL.Conv2D(64, (3, 3), padding='same')) model.add(KL.Activation('relu')) model.add(KL.Conv2D(64, (3, 3))) model.add(KL.Activation('relu')) model.add(KL.MaxPooling2D(pool_size=(2, 2))) model.add(KL.Dropout(0.25)) model.add(KL.Flatten()) model.add(KL.Dense(512)) model.add(KL.Activation('relu')) model.add(KL.Dropout(0.5)) model.add(KL.Dense(num_classes)) model.add(KL.Activation('softmax')) if weights_file is not None and os.path.exists(weights_file): model.load_weights(weights_file) return model
def train(train_generator,train_size,input_num,dims_num): print("Start Train Job! ") start=time.time() inputs=InputLayer(input_shape=(input_num,dims_num),batch_size=batch_size) layer1=Conv1D(64,3,activation="relu") layer2=Conv1D(64,3,activation="relu") layer3=Conv1D(128,3,activation="relu") layer4=Conv1D(128,3,activation="relu") layer5=Dense(128,activation="relu") output=Dense(2,activation="softmax",name="Output") optimizer=Adam() model=Sequential() model.add(inputs) model.add(layer1) model.add(layer2) model.add(MaxPool1D(pool_size=2)) model.add(Dropout(0.5)) model.add(layer3) model.add(layer4) model.add(MaxPool1D(pool_size=2)) model.add(Dropout(0.5)) model.add(Flatten()) model.add(layer5) model.add(Dropout(0.5)) model.add(output) call=TensorBoard(log_dir=log_dir,write_grads=True,histogram_freq=1) model.compile(optimizer,loss="categorical_crossentropy",metrics=["accuracy"]) model.fit_generator(train_generator,steps_per_epoch=train_size//batch_size,epochs=epochs_num,callbacks=[call]) # model.fit_generator(train_generator, steps_per_epoch=5, epochs=5, callbacks=[call]) model.save(model_dir) end=time.time() print("Over train job in %f s"%(end-start))
def bicubic(x, scale=3): model = Sequential() model.add(InputLayer(input_shape=x.shape[-3:])) model.add(ImageRescale(scale, method=tf.image.ResizeMethod.BICUBIC)) return model