def build_discriminator(self, k=4): inputs = Input(shape=self.input_shape) x = inputs x = conv(f=128, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=256, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=256, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=512, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) f = Flatten()(x) x = Dense(1024, kernel_regularizer=l2(0.001))(f) x = LeakyReLU(0.2)(x) x = Dense(1, activation='sigmoid', kernel_regularizer=l2(0.001))(x) return Model(inputs, [x, f])
def build_classifier(self, k=4): inputs = Input(shape=self.input_shape) x = inputs x = conv(f=128, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=256, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=256, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=512, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) f = Flatten()(x) x = Dense(1024, kernel_regularizer=l2(0.001))(f) x = LeakyReLU(0.2)(x) x = Dense(self.num_attrs, activation='softmax', kernel_regularizer=l2(0.001))(x) return Model(inputs, [x, f])
def build_encoder(self, output_dims, k=4): x_inputs = Input(shape=self.input_shape) c_inputs = Input(shape=(self.num_attrs, )) c = Reshape((1, 1, self.num_attrs))(c_inputs) c = UpSampling2D(size=self.input_shape[:2])(c) x = Concatenate(axis=-1)([x_inputs, c]) x = conv(f=128, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=256, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=256, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = conv(f=512, k=k, stride=2)(x) x = LeakyReLU(0.2)(x) x = Flatten()(x) x = Dense(1024, kernel_regularizer=l2(0.001))(x) x = LeakyReLU(0.2)(x) x = Dense(output_dims, kernel_regularizer=l2(0.001))(x) return Model([x_inputs, c_inputs], x)
def __init__(self, conv_dim=64, noise_dim=32, init_zero_weights=False): super(CycleGenerator, self).__init__() self.conv_dim = conv_dim self.noise_dim = noise_dim # 1. Define the encoder part of the generator (that extracts features from the input image) self.conv1 = conv(3, conv_dim, 4, padding=1, stride=2) self.conv2 = conv(conv_dim, conv_dim * 2, 4, padding=1, stride=2) # 2. Define the transformation part of the generator self.resnet_block = ResnetBlock(conv_dim * 2) # 3. Define the decoder part of the generator (that builds up the output image from features) self.deconv1 = deconv(conv_dim * 2 + noise_dim, conv_dim, 4, padding=1, stride=2) self.deconv2 = deconv(conv_dim, 3, 4, padding=1, stride=2, batch_norm=False)
from models import conv, xception import numpy as np import time import tensorflow as tf from tensorflow.keras.callbacks import TensorBoard tensorboard_cb = TensorBoard(log_dir="logs/", profile_batch=0) WIDTH = 80 HEIGHT = 60 LEARNING_RATE = 0.001 EPOCHS = 100 BATCH_SIZE = 75 MODEL_NAME = f"SelfDrivingCar-{time.time()}.h5" model = conv(WIDTH, HEIGHT, LEARNING_RATE) train_data = np.load("balanced_data.npy", allow_pickle=True) train = train_data[:-100] test = train_data[-100:] x_train = np.array([sample[0] for sample in train], dtype="int32").reshape(-1, WIDTH, HEIGHT, 1) y_train = np.array([sample[1] for sample in train], dtype="int32") x_test = np.array([sample[0] for sample in test], dtype="int32").reshape(-1, WIDTH, HEIGHT, 1) y_test = np.array([sample[1] for sample in test], dtype="int32") print(x_train)