{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":29849,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!python --version","metadata":{"execution":{"iopub.status.busy":"2024-01-20T05:16:26.077294Z","iopub.execute_input":"2024-01-20T05:16:26.078017Z","iopub.status.idle":"2024-01-20T05:16:27.205995Z","shell.execute_reply.started":"2024-01-20T05:16:26.077934Z","shell.execute_reply":"2024-01-20T05:16:27.204785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/davidADSP/GDL_code","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:26:01.448154Z","iopub.execute_input":"2024-03-19T03:26:01.448665Z","iopub.status.idle":"2024-03-19T03:26:03.587777Z","shell.execute_reply.started":"2024-03-19T03:26:01.448612Z","shell.execute_reply":"2024-03-19T03:26:03.586543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/GDL_code","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:26:47.980076Z","iopub.execute_input":"2024-03-19T03:26:47.980469Z","iopub.status.idle":"2024-03-19T03:26:47.988471Z","shell.execute_reply.started":"2024-03-19T03:26:47.980412Z","shell.execute_reply":"2024-03-19T03:26:47.987553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir gan","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:26:55.251844Z","iopub.execute_input":"2024-03-19T03:26:55.252473Z","iopub.status.idle":"2024-03-19T03:26:56.253987Z","shell.execute_reply.started":"2024-03-19T03:26:55.252189Z","shell.execute_reply":"2024-03-19T03:26:56.2529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd gan","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:27:01.166355Z","iopub.execute_input":"2024-03-19T03:27:01.166969Z","iopub.status.idle":"2024-03-19T03:27:01.174934Z","shell.execute_reply.started":"2024-03-19T03:27:01.166875Z","shell.execute_reply":"2024-03-19T03:27:01.173884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom keras.layers import Input, Conv2D, Flatten, Dense, Conv2DTranspose, Reshape, Lambda, Activation, BatchNormalization, LeakyReLU, Dropout, ZeroPadding2D, UpSampling2D\nfrom keras.layers.merge import _Merge\n\nfrom keras.models import Model, Sequential\nfrom keras import backend as K\nfrom keras.optimizers import Adam, RMSprop\nfrom keras.utils import plot_model\nfrom keras.initializers import RandomNormal\n\nimport numpy as np\nimport json\nimport os\nimport pickle as pkl\nimport matplotlib.pyplot as plt\n\n\nclass GAN():\n    def __init__(self\n        , input_dim\n        , discriminator_conv_filters\n        , discriminator_conv_kernel_size\n        , discriminator_conv_strides\n        , discriminator_batch_norm_momentum\n        , discriminator_activation\n        , discriminator_dropout_rate\n        , discriminator_learning_rate\n        , generator_initial_dense_layer_size\n        , generator_upsample\n        , generator_conv_filters\n        , generator_conv_kernel_size\n        , generator_conv_strides\n        , generator_batch_norm_momentum\n        , generator_activation\n        , generator_dropout_rate\n        , generator_learning_rate\n        , optimiser\n        , z_dim\n        ):\n\n        self.name = 'gan'\n\n        self.input_dim = input_dim\n        self.discriminator_conv_filters = discriminator_conv_filters\n        self.discriminator_conv_kernel_size = discriminator_conv_kernel_size\n        self.discriminator_conv_strides = discriminator_conv_strides\n        self.discriminator_batch_norm_momentum = discriminator_batch_norm_momentum\n        self.discriminator_activation = discriminator_activation\n        self.discriminator_dropout_rate = discriminator_dropout_rate\n        self.discriminator_learning_rate = discriminator_learning_rate\n\n        self.generator_initial_dense_layer_size = generator_initial_dense_layer_size\n        self.generator_upsample = generator_upsample\n        self.generator_conv_filters = generator_conv_filters\n        self.generator_conv_kernel_size = generator_conv_kernel_size\n        self.generator_conv_strides = generator_conv_strides\n        self.generator_batch_norm_momentum = generator_batch_norm_momentum\n        self.generator_activation = generator_activation\n        self.generator_dropout_rate = generator_dropout_rate\n        self.generator_learning_rate = generator_learning_rate\n        \n        self.optimiser = optimiser\n        self.z_dim = z_dim\n\n        self.n_layers_discriminator = len(discriminator_conv_filters)\n        self.n_layers_generator = len(generator_conv_filters)\n\n        self.weight_init = RandomNormal(mean=0., stddev=0.02)\n\n        self.d_losses = []\n        self.g_losses = []\n\n        self.epoch = 0\n\n        self._build_discriminator()\n        self._build_generator()\n\n        self._build_adversarial()\n\n    def get_activation(self, activation):\n        if activation == 'leaky_relu':\n            layer = LeakyReLU(alpha = 0.2)\n        else:\n            layer = Activation(activation)\n        return layer\n\n    def _build_discriminator(self):\n\n        ### THE discriminator\n        discriminator_input = Input(shape=self.input_dim, name='discriminator_input')\n\n        x = discriminator_input\n\n        for i in range(self.n_layers_discriminator):\n\n            x = Conv2D(\n                filters = self.discriminator_conv_filters[i]\n                , kernel_size = self.discriminator_conv_kernel_size[i]\n                , strides = self.discriminator_conv_strides[i]\n                , padding = 'same'\n                , name = 'discriminator_conv_' + str(i)\n                , kernel_initializer = self.weight_init\n                )(x)\n\n            if self.discriminator_batch_norm_momentum and i > 0:\n                x = BatchNormalization(momentum = self.discriminator_batch_norm_momentum)(x)\n\n            x = self.get_activation(self.discriminator_activation)(x)\n\n            if self.discriminator_dropout_rate:\n                x = Dropout(rate = self.discriminator_dropout_rate)(x)\n\n        x = Flatten()(x)\n        \n        discriminator_output = Dense(1, activation='sigmoid', kernel_initializer = self.weight_init)(x)\n\n        self.discriminator = Model(discriminator_input, discriminator_output)\n\n\n    def _build_generator(self):\n\n        ### THE generator\n\n        generator_input = Input(shape=(self.z_dim,), name='generator_input')\n\n        x = generator_input\n\n        x = Dense(np.prod(self.generator_initial_dense_layer_size), kernel_initializer = self.weight_init)(x)\n\n        if self.generator_batch_norm_momentum:\n            x = BatchNormalization(momentum = self.generator_batch_norm_momentum)(x)\n\n        x = self.get_activation(self.generator_activation)(x)\n\n        x = Reshape(self.generator_initial_dense_layer_size)(x)\n\n        if self.generator_dropout_rate:\n            x = Dropout(rate = self.generator_dropout_rate)(x)\n\n        for i in range(self.n_layers_generator):\n\n            if self.generator_upsample[i] == 2:\n                x = UpSampling2D()(x)\n                x = Conv2D(\n                    filters = self.generator_conv_filters[i]\n                    , kernel_size = self.generator_conv_kernel_size[i]\n                    , padding = 'same'\n                    , name = 'generator_conv_' + str(i)\n                    , kernel_initializer = self.weight_init\n                )(x)\n            else:\n\n                x = Conv2DTranspose(\n                    filters = self.generator_conv_filters[i]\n                    , kernel_size = self.generator_conv_kernel_size[i]\n                    , padding = 'same'\n                    , strides = self.generator_conv_strides[i]\n                    , name = 'generator_conv_' + str(i)\n                    , kernel_initializer = self.weight_init\n                    )(x)\n\n            if i < self.n_layers_generator - 1:\n\n                if self.generator_batch_norm_momentum:\n                    x = BatchNormalization(momentum = self.generator_batch_norm_momentum)(x)\n\n                x = self.get_activation(self.generator_activation)(x)\n                    \n                \n            else:\n\n                x = Activation('tanh')(x)\n\n\n        generator_output = x\n\n        self.generator = Model(generator_input, generator_output)\n\n       \n    def get_opti(self, lr):\n        if self.optimiser == 'adam':\n            opti = Adam(lr=lr, beta_1=0.5)\n        elif self.optimiser == 'rmsprop':\n            opti = RMSprop(lr=lr)\n        else:\n            opti = Adam(lr=lr)\n\n        return opti\n\n    def set_trainable(self, m, val):\n        m.trainable = val\n        for l in m.layers:\n            l.trainable = val\n\n\n    def _build_adversarial(self):\n        \n        ### COMPILE DISCRIMINATOR\n\n        self.discriminator.compile(\n        optimizer=self.get_opti(self.discriminator_learning_rate)  \n        , loss = 'binary_crossentropy'\n        ,  metrics = ['accuracy']\n        )\n        \n        ### COMPILE THE FULL GAN\n\n        self.set_trainable(self.discriminator, False)\n\n        model_input = Input(shape=(self.z_dim,), name='model_input')\n        model_output = self.discriminator(self.generator(model_input))\n        self.model = Model(model_input, model_output)\n\n        self.model.compile(optimizer=self.get_opti(self.generator_learning_rate) , loss='binary_crossentropy', metrics=['accuracy'])\n\n        self.set_trainable(self.discriminator, True)\n\n\n\n    \n    def train_discriminator(self, x_train, batch_size, using_generator):\n\n        valid = np.ones((batch_size,1))\n        fake = np.zeros((batch_size,1))\n\n        if using_generator:\n            true_imgs = next(x_train)[0]\n            if true_imgs.shape[0] != batch_size:\n                true_imgs = next(x_train)[0]\n        else:\n            idx = np.random.randint(0, x_train.shape[0], batch_size)\n            true_imgs = x_train[idx]\n        \n        noise = np.random.normal(0, 1, (batch_size, self.z_dim))\n        gen_imgs = self.generator.predict(noise)\n\n        d_loss_real, d_acc_real =   self.discriminator.train_on_batch(true_imgs, valid)\n        d_loss_fake, d_acc_fake =   self.discriminator.train_on_batch(gen_imgs, fake)\n        d_loss =  0.5 * (d_loss_real + d_loss_fake)\n        d_acc = 0.5 * (d_acc_real + d_acc_fake)\n\n        return [d_loss, d_loss_real, d_loss_fake, d_acc, d_acc_real, d_acc_fake]\n\n    def train_generator(self, batch_size):\n        valid = np.ones((batch_size,1))\n        noise = np.random.normal(0, 1, (batch_size, self.z_dim))\n        return self.model.train_on_batch(noise, valid)\n\n\n    def train(self, x_train, batch_size, epochs, run_folder\n    , print_every_n_batches = 50\n    , using_generator = False):\n\n        for epoch in range(self.epoch, self.epoch + epochs):\n\n            d = self.train_discriminator(x_train, batch_size, using_generator)\n            g = self.train_generator(batch_size)\n\n            print (\"%d [D loss: (%.3f)(R %.3f, F %.3f)] [D acc: (%.3f)(%.3f, %.3f)] [G loss: %.3f] [G acc: %.3f]\" % (epoch, d[0], d[1], d[2], d[3], d[4], d[5], g[0], g[1]))\n\n            self.d_losses.append(d)\n            self.g_losses.append(g)\n\n            if epoch % print_every_n_batches == 0:\n                self.sample_images(run_folder)\n                self.model.save_weights(os.path.join(run_folder, 'weights/weights-%d.h5' % (epoch)))\n                self.model.save_weights(os.path.join(run_folder, 'weights/weights.h5'))\n                self.save_model(run_folder)\n\n            self.epoch += 1\n\n    \n    def sample_images(self, run_folder):\n        r, c = 5, 5\n        noise = np.random.normal(0, 1, (r * c, self.z_dim))\n        gen_imgs = self.generator.predict(noise)\n\n        gen_imgs = 0.5 * (gen_imgs + 1)\n        gen_imgs = np.clip(gen_imgs, 0, 1)\n\n        fig, axs = plt.subplots(r, c, figsize=(15,15))\n        cnt = 0\n\n        for i in range(r):\n            for j in range(c):\n                axs[i,j].imshow(np.squeeze(gen_imgs[cnt, :,:,:]), cmap = 'gray')\n                axs[i,j].axis('off')\n                cnt += 1\n        fig.savefig(os.path.join(run_folder, \"images/sample_%d.png\" % self.epoch))\n        plt.close()\n\n\n\n\n    \n    def plot_model(self, run_folder):\n        plot_model(self.model, to_file=os.path.join(run_folder ,'viz/model.png'), show_shapes = True, show_layer_names = True)\n        plot_model(self.discriminator, to_file=os.path.join(run_folder ,'viz/discriminator.png'), show_shapes = True, show_layer_names = True)\n        plot_model(self.generator, to_file=os.path.join(run_folder ,'viz/generator.png'), show_shapes = True, show_layer_names = True)\n\n\n\n    def save(self, folder):\n\n        with open(os.path.join(folder, 'params.pkl'), 'wb') as f:\n            pkl.dump([\n                self.input_dim\n                , self.discriminator_conv_filters\n                , self.discriminator_conv_kernel_size\n                , self.discriminator_conv_strides\n                , self.discriminator_batch_norm_momentum\n                , self.discriminator_activation\n                , self.discriminator_dropout_rate\n                , self.discriminator_learning_rate\n                , self.generator_initial_dense_layer_size\n                , self.generator_upsample\n                , self.generator_conv_filters\n                , self.generator_conv_kernel_size\n                , self.generator_conv_strides\n                , self.generator_batch_norm_momentum\n                , self.generator_activation\n                , self.generator_dropout_rate\n                , self.generator_learning_rate\n                , self.optimiser\n                , self.z_dim\n                ], f)\n\n        self.plot_model(folder)\n\n    def save_model(self, run_folder):\n        self.model.save(os.path.join(run_folder, 'model.h5'))\n        self.discriminator.save(os.path.join(run_folder, 'discriminator.h5'))\n        self.generator.save(os.path.join(run_folder, 'generator.h5'))\n        pkl.dump(self, open( os.path.join(run_folder, \"obj.pkl\"), \"wb\" ))\n\n    def load_weights(self, filepath):\n        self.model.load_weights(filepath)\n\n\n        \n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:27:05.631194Z","iopub.execute_input":"2024-03-19T03:27:05.631845Z","iopub.status.idle":"2024-03-19T03:27:12.996499Z","shell.execute_reply.started":"2024-03-19T03:27:05.63179Z","shell.execute_reply":"2024-03-19T03:27:12.995362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nimport os\n\nfrom keras.datasets import mnist, cifar100,cifar10\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, save_img, img_to_array\n\nimport pandas as pd\n\nimport numpy as np\nfrom os import walk, getcwd\nimport h5py\n\nimport scipy\nfrom glob import glob\n\nfrom keras.applications import vgg19\nfrom keras import backend as K\nfrom keras.utils import to_categorical\n\nimport pdb\n\n\nclass ImageLabelLoader():\n    def __init__(self, image_folder, target_size):\n        self.image_folder = image_folder\n        self.target_size = target_size\n\n    def build(self, att, batch_size, label = None):\n\n        data_gen = ImageDataGenerator(rescale=1./255)\n        if label:\n            data_flow = data_gen.flow_from_dataframe(\n                att\n                , self.image_folder\n                , x_col='image_id'\n                , y_col=label\n                , target_size=self.target_size \n                , class_mode='other'\n                , batch_size=batch_size\n                , shuffle=True\n            )\n        else:\n            data_flow = data_gen.flow_from_dataframe(\n                att\n                , self.image_folder\n                , x_col='image_id'\n                , target_size=self.target_size \n                , class_mode='input'\n                , batch_size=batch_size\n                , shuffle=True\n            )\n\n        return data_flow\n\n\n\n\nclass DataLoader():\n    def __init__(self, dataset_name, img_res=(256, 256)):\n        self.dataset_name = dataset_name\n        self.img_res = img_res\n\n    def load_data(self, domain, batch_size=1, is_testing=False):\n        data_type = \"train%s\" % domain if not is_testing else \"test%s\" % domain\n        path = glob('./data/%s/%s/*' % (self.dataset_name, data_type))\n\n        batch_images = np.random.choice(path, size=batch_size)\n\n        imgs = []\n        for img_path in batch_images:\n            img = self.imread(img_path)\n            if not is_testing:\n                img = scipy.misc.imresize(img, self.img_res)\n\n                if np.random.random() > 0.5:\n                    img = np.fliplr(img)\n            else:\n                img = scipy.misc.imresize(img, self.img_res)\n            imgs.append(img)\n\n        imgs = np.array(imgs)/127.5 - 1.\n\n        return imgs\n\n    def load_batch(self, batch_size=1, is_testing=False):\n        data_type = \"train\" if not is_testing else \"val\"\n        path_A = glob('./data/%s/%sA/*' % (self.dataset_name, data_type))\n        path_B = glob('./data/%s/%sB/*' % (self.dataset_name, data_type))\n\n        self.n_batches = int(min(len(path_A), len(path_B)) / batch_size)\n        total_samples = self.n_batches * batch_size\n\n        # Sample n_batches * batch_size from each path list so that model sees all\n        # samples from both domains\n        path_A = np.random.choice(path_A, total_samples, replace=False)\n        path_B = np.random.choice(path_B, total_samples, replace=False)\n\n        for i in range(self.n_batches-1):\n            batch_A = path_A[i*batch_size:(i+1)*batch_size]\n            batch_B = path_B[i*batch_size:(i+1)*batch_size]\n            imgs_A, imgs_B = [], []\n            for img_A, img_B in zip(batch_A, batch_B):\n                img_A = self.imread(img_A)\n                img_B = self.imread(img_B)\n\n                img_A = scipy.misc.imresize(img_A, self.img_res)\n                img_B = scipy.misc.imresize(img_B, self.img_res)\n\n                if not is_testing and np.random.random() > 0.5:\n                        img_A = np.fliplr(img_A)\n                        img_B = np.fliplr(img_B)\n\n                imgs_A.append(img_A)\n                imgs_B.append(img_B)\n\n            imgs_A = np.array(imgs_A)/127.5 - 1.\n            imgs_B = np.array(imgs_B)/127.5 - 1.\n\n            yield imgs_A, imgs_B\n\n    def load_img(self, path):\n        img = self.imread(path)\n        img = scipy.misc.imresize(img, self.img_res)\n        img = img/127.5 - 1.\n        return img[np.newaxis, :, :, :]\n\n    def imread(self, path):\n        return scipy.misc.imread(path, mode='RGB').astype(np.float)\n\n\n\n\ndef load_model(model_class, folder):\n    \n    with open(os.path.join(folder, 'params.pkl'), 'rb') as f:\n        params = pickle.load(f)\n\n    model = model_class(*params)\n\n    model.load_weights(os.path.join(folder, 'weights/weights.h5'))\n\n    return model\n\n\ndef load_mnist():\n    (x_train, y_train), (x_test, y_test) = mnist.load_data()\n\n    x_train = x_train.astype('float32') / 255.\n    x_train = x_train.reshape(x_train.shape + (1,))\n    x_test = x_test.astype('float32') / 255.\n    x_test = x_test.reshape(x_test.shape + (1,))\n\n    return (x_train, y_train), (x_test, y_test)\n\ndef load_mnist_gan():\n    (x_train, y_train), (x_test, y_test) = mnist.load_data()\n\n    x_train = (x_train.astype('float32') - 127.5) / 127.5\n    x_train = x_train.reshape(x_train.shape + (1,))\n    x_test = (x_test.astype('float32') - 127.5) / 127.5\n    x_test = x_test.reshape(x_test.shape + (1,))\n\n    return (x_train, y_train), (x_test, y_test)\n\n\n\ndef load_fashion_mnist(input_rows, input_cols, path='./data/fashion/fashion-mnist_train.csv'):\n    #read the csv data\n    df = pd.read_csv(path)\n    #extract the image pixels\n    X_train = df.drop(columns = ['label'])\n    X_train = X_train.values\n    X_train = (X_train.astype('float32') - 127.5) / 127.5\n    X_train = X_train.reshape(X_train.shape[0], input_rows, input_cols, 1)\n    #extract the labels\n    y_train = df['label'].values\n    \n    return X_train, y_train\n\ndef load_safari(folder):\n\n    mypath = os.path.join(\"./data\", folder)\n    txt_name_list = []\n    for (dirpath, dirnames, filenames) in walk(mypath):\n        for f in filenames:\n            if f != '.DS_Store':\n                txt_name_list.append(f)\n                break\n\n    slice_train = int(80000/len(txt_name_list))  ###Setting value to be 80000 for the final dataset\n    i = 0\n    seed = np.random.randint(1, 10e6)\n\n    for txt_name in txt_name_list:\n        txt_path = os.path.join(mypath,txt_name)\n        x = np.load(txt_path)\n        x = (x.astype('float32') - 127.5) / 127.5\n        # x = x.astype('float32') / 255.0\n        \n        x = x.reshape(x.shape[0], 28, 28, 1)\n        \n        y = [i] * len(x)  \n        np.random.seed(seed)\n        np.random.shuffle(x)\n        np.random.seed(seed)\n        np.random.shuffle(y)\n        x = x[:slice_train]\n        y = y[:slice_train]\n        if i != 0: \n            xtotal = np.concatenate((x,xtotal), axis=0)\n            ytotal = np.concatenate((y,ytotal), axis=0)\n        else:\n            xtotal = x\n            ytotal = y\n        i += 1\n        \n    return xtotal, ytotal\n\n\n\ndef load_cifar(label, num):\n    if num == 10:\n        (x_train, y_train), (x_test, y_test) = cifar10.load_data()\n    else:\n        (x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode = 'fine')\n\n    train_mask = [y[0]==label for y in y_train]\n    test_mask = [y[0]==label for y in y_test]\n\n    x_data = np.concatenate([x_train[train_mask], x_test[test_mask]])\n    y_data = np.concatenate([y_train[train_mask], y_test[test_mask]])\n\n    x_data = (x_data.astype('float32') - 127.5) / 127.5\n \n    return (x_data, y_data)\n\n\ndef load_celeb(data_name, image_size, batch_size):\n    data_folder = os.path.join(\"./data\", data_name)\n\n    data_gen = ImageDataGenerator(preprocessing_function=lambda x: (x.astype('float32') - 127.5) / 127.5)\n\n    x_train = data_gen.flow_from_directory(data_folder\n                                            , target_size = (image_size,image_size)\n                                            , batch_size = batch_size\n                                            , shuffle = True\n                                            , class_mode = 'input'\n                                            , subset = \"training\"\n                                                )\n\n    return x_train\n\n\ndef load_music(data_name, filename, n_bars, n_steps_per_bar):\n    file = os.path.join(\"./data\", data_name, filename)\n\n    with np.load(file, encoding='bytes') as f:\n        data = f['train']\n\n    data_ints = []\n\n    for x in data:\n        counter = 0\n        cont = True\n        while cont:\n            if not np.any(np.isnan(x[counter:(counter+4)])):\n                cont = False\n            else:\n                counter += 4\n\n        if n_bars * n_steps_per_bar < x.shape[0]:\n            data_ints.append(x[counter:(counter + (n_bars * n_steps_per_bar)),:])\n\n\n    data_ints = np.array(data_ints)\n\n    n_songs = data_ints.shape[0]\n    n_tracks = data_ints.shape[2]\n\n    data_ints = data_ints.reshape([n_songs, n_bars, n_steps_per_bar, n_tracks])\n\n    max_note = 83\n\n    where_are_NaNs = np.isnan(data_ints)\n    data_ints[where_are_NaNs] = max_note + 1\n    max_note = max_note + 1\n\n    data_ints = data_ints.astype(int)\n\n    num_classes = max_note + 1\n\n    \n    data_binary = np.eye(num_classes)[data_ints]\n    data_binary[data_binary==0] = -1\n    data_binary = np.delete(data_binary, max_note,-1)\n\n    data_binary = data_binary.transpose([0,1,2, 4,3])\n    \n    \n\n    \n\n    return data_binary, data_ints, data\n\n\ndef preprocess_image(data_name, file, img_nrows, img_ncols):\n\n    image_path = os.path.join('./data', data_name, file)\n\n    img = load_img(image_path, target_size=(img_nrows, img_ncols))\n    img = img_to_array(img)\n    img = np.expand_dims(img, axis=0)\n    img = vgg19.preprocess_input(img)\n    return img\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:27:44.967683Z","iopub.execute_input":"2024-03-19T03:27:44.968311Z","iopub.status.idle":"2024-03-19T03:27:45.030866Z","shell.execute_reply.started":"2024-03-19T03:27:44.968235Z","shell.execute_reply":"2024-03-19T03:27:45.030085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:28:01.51783Z","iopub.execute_input":"2024-03-19T03:28:01.518429Z","iopub.status.idle":"2024-03-19T03:28:01.523419Z","shell.execute_reply.started":"2024-03-19T03:28:01.518364Z","shell.execute_reply":"2024-03-19T03:28:01.522439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ..","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:29:00.918242Z","iopub.execute_input":"2024-03-19T03:29:00.918581Z","iopub.status.idle":"2024-03-19T03:29:00.924378Z","shell.execute_reply.started":"2024-03-19T03:29:00.918541Z","shell.execute_reply":"2024-03-19T03:29:00.923281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/GDL_code/data","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:09:08.324439Z","iopub.execute_input":"2024-03-19T04:09:08.324889Z","iopub.status.idle":"2024-03-19T04:09:08.332523Z","shell.execute_reply.started":"2024-03-19T04:09:08.324816Z","shell.execute_reply":"2024-03-19T04:09:08.331397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir camel","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:09:19.851821Z","iopub.execute_input":"2024-03-19T04:09:19.852198Z","iopub.status.idle":"2024-03-19T04:09:20.87492Z","shell.execute_reply.started":"2024-03-19T04:09:19.852134Z","shell.execute_reply":"2024-03-19T04:09:20.873636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ..","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:09:25.381204Z","iopub.execute_input":"2024-03-19T04:09:25.381575Z","iopub.status.idle":"2024-03-19T04:09:25.388349Z","shell.execute_reply.started":"2024-03-19T04:09:25.381521Z","shell.execute_reply":"2024-03-19T04:09:25.387299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.move('/kaggle/working/GDL_code/camel.npy','/kaggle/working/GDL_code/data/camel')","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:10:05.500314Z","iopub.execute_input":"2024-03-19T04:10:05.500945Z","iopub.status.idle":"2024-03-19T04:10:05.507454Z","shell.execute_reply.started":"2024-03-19T04:10:05.500869Z","shell.execute_reply":"2024-03-19T04:10:05.50647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run params\nSECTION = 'gan'\nRUN_ID = '0001'\nDATA_NAME = 'camel'\nRUN_FOLDER = 'run/{}/'.format(SECTION)\nRUN_FOLDER += '_'.join([RUN_ID, DATA_NAME])\n\nif not os.path.exists(RUN_FOLDER):\n    os.mkdir(RUN_FOLDER)\n    os.mkdir(os.path.join(RUN_FOLDER, 'viz'))\n    os.mkdir(os.path.join(RUN_FOLDER, 'images'))\n    os.mkdir(os.path.join(RUN_FOLDER, 'weights'))\n\nmode =  'build' #'load' #","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:10:12.151595Z","iopub.execute_input":"2024-03-19T04:10:12.152008Z","iopub.status.idle":"2024-03-19T04:10:12.160445Z","shell.execute_reply.started":"2024-03-19T04:10:12.151931Z","shell.execute_reply":"2024-03-19T04:10:12.159167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train, y_train) = load_safari(DATA_NAME)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:10:19.162092Z","iopub.execute_input":"2024-03-19T04:10:19.162448Z","iopub.status.idle":"2024-03-19T04:10:20.152791Z","shell.execute_reply.started":"2024-03-19T04:10:19.162397Z","shell.execute_reply":"2024-03-19T04:10:20.15166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/GDL_code","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:51:37.164497Z","iopub.execute_input":"2024-03-19T03:51:37.164876Z","iopub.status.idle":"2024-03-19T03:51:37.171199Z","shell.execute_reply.started":"2024-03-19T03:51:37.16482Z","shell.execute_reply":"2024-03-19T03:51:37.170227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf=pd.read_csv('camel.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-19T03:50:12.399392Z","iopub.execute_input":"2024-03-19T03:50:12.399895Z","iopub.status.idle":"2024-03-19T03:50:13.877076Z","shell.execute_reply.started":"2024-03-19T03:50:12.399827Z","shell.execute_reply":"2024-03-19T03:50:13.876107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[200,:,:,0], cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:11:49.820149Z","iopub.execute_input":"2024-03-19T04:11:49.82079Z","iopub.status.idle":"2024-03-19T04:11:50.045936Z","shell.execute_reply.started":"2024-03-19T04:11:49.820728Z","shell.execute_reply":"2024-03-19T04:11:50.044698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gan = GAN(input_dim = (28,28,1)\n        , discriminator_conv_filters = [64,64,128,128]\n        , discriminator_conv_kernel_size = [5,5,5,5]\n        , discriminator_conv_strides = [2,2,2,1]\n        , discriminator_batch_norm_momentum = None\n        , discriminator_activation = 'relu'\n        , discriminator_dropout_rate = 0.4\n        , discriminator_learning_rate = 0.0008\n        , generator_initial_dense_layer_size = (7, 7, 64)\n        , generator_upsample = [2,2, 1, 1]\n        , generator_conv_filters = [128,64, 64,1]\n        , generator_conv_kernel_size = [5,5,5,5]\n        , generator_conv_strides = [1,1, 1, 1]\n        , generator_batch_norm_momentum = 0.9\n        , generator_activation = 'relu'\n        , generator_dropout_rate = None\n        , generator_learning_rate = 0.0004\n        , optimiser = 'rmsprop'\n        , z_dim = 100\n        )\n\nif mode == 'build':\n    gan.save(RUN_FOLDER)\nelse:\n    gan.load_weights(os.path.join(RUN_FOLDER, 'weights/weights.h5'))","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:11:52.36176Z","iopub.execute_input":"2024-03-19T04:11:52.362125Z","iopub.status.idle":"2024-03-19T04:11:53.618828Z","shell.execute_reply.started":"2024-03-19T04:11:52.362071Z","shell.execute_reply":"2024-03-19T04:11:53.617461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\nEPOCHS = 6000\nPRINT_EVERY_N_BATCHES = 5","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:10:42.976152Z","iopub.execute_input":"2024-03-19T04:10:42.976551Z","iopub.status.idle":"2024-03-19T04:10:42.982126Z","shell.execute_reply.started":"2024-03-19T04:10:42.976477Z","shell.execute_reply":"2024-03-19T04:10:42.98087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gan.trainable=True\ngan.train(     \n    x_train\n    , batch_size = BATCH_SIZE\n    , epochs = EPOCHS\n    , run_folder = RUN_FOLDER\n    , print_every_n_batches = PRINT_EVERY_N_BATCHES\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:14:43.810118Z","iopub.execute_input":"2024-03-19T04:14:43.810542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nplt.plot([x[0] for x in gan.d_losses], color='black', linewidth=0.25)\n\nplt.plot([x[1] for x in gan.d_losses], color='green', linewidth=0.25)\nplt.plot([x[2] for x in gan.d_losses], color='red', linewidth=0.25)\nplt.plot([x[0] for x in gan.g_losses], color='orange', linewidth=0.25)\n\nplt.xlabel('batch', fontsize=18)\nplt.ylabel('loss', fontsize=16)\n\nplt.xlim(0, 2000)\nplt.ylim(0, 2)\n\nplt.show()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install gsutil ","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:02:37.899814Z","iopub.execute_input":"2024-03-19T04:02:37.900247Z","iopub.status.idle":"2024-03-19T04:03:26.441979Z","shell.execute_reply.started":"2024-03-19T04:02:37.900192Z","shell.execute_reply":"2024-03-19T04:03:26.440971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gsutil -m cp -r \"gs://quickdraw_dataset/full/numpy_bitmap/camel.npy\" .","metadata":{"execution":{"iopub.status.busy":"2024-03-19T04:07:11.550693Z","iopub.execute_input":"2024-03-19T04:07:11.551051Z","iopub.status.idle":"2024-03-19T04:07:15.181757Z","shell.execute_reply.started":"2024-03-19T04:07:11.551003Z","shell.execute_reply":"2024-03-19T04:07:15.180221Z"},"trusted":true},"execution_count":null,"outputs":[]}]}