<jupyter_start><jupyter_text>Train upper_base + Evaluation<jupyter_code>!pip install -q kaggle
pip install 'h5py==2.10.0' --force-reinstall
!pip install ISR
!pip install --upgrade tensorflow-gpu
!pip install --upgrade keras
!mkdir ~/.kaggle
!cp kaggle.json ~/.kaggle
!chmod 600 ~/.kaggle/kaggle.json
!kaggle datasets download -d congthjnh/aiijc-new128x128
!unzip -qq "/content/aiijc-new128x128.zip" -d "/content/aiijc-new128x128 (train)"
from IPython.display import Image, display
import numpy as np
import os
import tensorflow
import math
import random
from PIL import ImageFile, Image
import pandas as pd
from tensorflow.keras.models import *
from tensorflow.keras.layers import *
from tensorflow.keras.preprocessing.image import *
import matplotlib.pyplot as plt
import re
from tqdm import tqdm
import pickle
import tensorflow as tf
from tensorflow.keras.applications import *
from tensorflow.keras import *
import tensorflow as tf 
import cv2
ImageFile.LOAD_TRUNCATED_IMAGES = True
%matplotlib inline
from shutil import copyfile
import glob
import imageio
import PIL
import time
import glob
import imageio
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
from tensorflow.keras import layers
import time

from IPython import display
root_dir = "/content/aiijc-new128x128 (train)/New folder"
class_names = os.listdir(root_dir)
class_names.sort()
class_names.pop()
x = []
y = []
z = []
X_train_paths = []
X_val_paths = []
y_val = []

for label in tqdm(class_names):
  img_names = os.listdir(root_dir + "/" + label)
  count = 0
  while count < 5:
    x.append(os.path.join(label, img_names[count]))
    y.append("sign")
    X_val_paths.append(os.path.join(label, img_names[count]))
    y_val.append(1)
    z.append("val")
    img_names.pop(0)
    count += 1
  for img_name in img_names:
    x.append(os.path.join(label, img_name))
    y.append("sign")
    X_train_paths.append(os.path.join(os.path.join(label, img_name)))
    z.append("train")

unknown_class = os.listdir(root_dir + "/" + "unknown")
for i in tqdm(range(len(unknown_class))):
  x.append(os.path.join("unknown", unknown_class[i]))
  y.append("unknown")
  X_val_paths.append(os.path.join("unknown", unknown_class[i]))
  y_val.append(0)
  z.append("val")
image_size = 64

def read_and_prep_images(img_paths, img_height = image_size, img_width = image_size):
    imgs = [load_img(os.path.join(root_dir,img_path), target_size = (img_height, img_width)) for img_path in tqdm(img_paths)]
    img_array = np.array([img_to_array(img) for img in imgs])
    output = img_array/255.0
    return(output)

X_train = read_and_prep_images(X_train_paths)
np.random.shuffle(X_train)
X_test = read_and_prep_images(X_val_paths)
BUFFER_SIZE = 45000
BATCH_SIZE = 64
train_dataset = tf.data.Dataset.from_tensor_slices(X_train).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
from ISR.models import RRDN
from ISR.models import Discriminator
from ISR.models import Cut_VGG19

lr_train_patch_size = 32
layers_to_extract = [5, 9]
scale = 2
hr_train_patch_size = lr_train_patch_size * scale

rrdn  = RRDN(arch_params = {'C':4, 'D':3, 'G':64, 'G0':64, 'T':10, 'x':scale}, patch_size=lr_train_patch_size)
f_ext = Cut_VGG19(patch_size = hr_train_patch_size, layers_to_extract = layers_to_extract)
discr = Discriminator(patch_size = hr_train_patch_size, kernel_size = 3)
rrdn.model.load_weights('/content/drive/MyDrive/Geoservice analysis/ISR/generator(4).hdf5')
discr.model.load_weights('/content/drive/MyDrive/Geoservice analysis/ISR/discriminator(4).hdf5')
gen_base = load_model('/content/drive/MyDrive/Geoservice analysis/ISR/gen_base.h5')
discr_base = load_model('/content/drive/MyDrive/Geoservice analysis/ISR/discr_base.h5')
rrdn.model.trainable = False

generator = Sequential([
    gen_base,
    rrdn.model                   
])
discr.model.trainable = False

discriminator = Sequential([
    discr.model,
    discr_base              
])
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=False)
def discriminator_loss(real_output, fake_output):
    real_loss = cross_entropy(tf.ones_like(real_output), real_output)
    fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
    total_loss = real_loss + fake_loss
    return total_loss
def generator_loss(fake_output):
    return cross_entropy(tf.ones_like(fake_output), fake_output)
generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
EPOCHS = 50
noise_dim = 100
num_examples_to_generate = 64

# You will reuse this seed overtime (so it's easier)
# to visualize progress in the animated GIF)
seed = tf.random.normal([num_examples_to_generate, noise_dim])
@tf.function #converts regular python code to a callable Tensorflow graph function
def train_step(images):
    noise = tf.random.normal([BATCH_SIZE, noise_dim]) #noise dim is the latent representation
    print('Done noise')

    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape: #one for generator, the other for discriminator
      generated_images = generator(noise, training = True) #generate a batch of fake images (from noise)
      print('Done generated images')

      real_output = discriminator(images, training = True)
      fake_output = discriminator(generated_images, training = True)
      print('Done fake output')

      gen_loss = generator_loss(fake_output)
      disc_loss = discriminator_loss(real_output, fake_output)

      print('Done loss')

    gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
    #gen loss is target, generator is the source (gradient tape will record all of the operatirons happen to peform backward gradient)
    gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

    print('Done gradients')

    generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
    discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))

    print('Done opt')
def train(dataset, epochs):
  for epoch in range(epochs):
    start = time.time()

    print('Start-timing')

    for image_batch in dataset:
      train_step(image_batch)
      print('1 step')
    
    print('---Done 1 epoch---')

    # Produce images for the GIF as you go
    # Save the model every 1 epochs
    gen_base.save('/content/drive/MyDrive/Geoservice analysis/gen_base.h5')
    discr_base.save('/content/drive/MyDrive/Geoservice analysis/discr_base.h5')
    generator.save('/content/drive/MyDrive/Geoservice analysis/big_generator.h5')
    discriminator.save('/content/drive/MyDrive/Geoservice analysis/big_discriminator.h5')

    print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))

      # Generate after the final epoch
  display.clear_output(wait = True)
  generate_and_save_images(generator,
                           epochs,
                           seed
                           )
def generate_and_save_images(model, epoch, test_input):
  # Notice `training` is set to False.
  # This is so all layers run in inference mode (batchnorm).
  predictions = model(test_input, training=False)

  fig = plt.figure(figsize = (8, 8))

  for i in range(predictions.shape[0]):
      plt.subplot(8, 8, i + 1)
      plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5)
      plt.axis('off')

  plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
  plt.show()
train(train_dataset, 5)
discr_base_val = load_model('/content/drive/MyDrive/Geoservice analysis/discr_base.h5')
discriminator_val = Sequential([
    discr.model,
    discr_base_val  
])
import sklearn
from sklearn.metrics import roc_auc_score
y_pred= discriminator_val.predict(X_test)
print(roc_auc_score(y_val, y_pred))<jupyter_output><empty_output><jupyter_text>Train ISR base<jupyter_code>!kaggle datasets download -d congthjnh/encoded-imgs
!unzip -qq "/content/encoded-imgs.zip" -d "/content/aiijc-80x80 (train)"
df = pd.read_csv('/content/train.csv')
df_uk = df.loc[df['label']=='unknown']
for i in tqdm(df_uk['filename']):
  df_uk.loc[df['filename']==i, 'filename']= i.replace('train_images/','')
for i in tqdm(df_uk['filename']):
  os.remove('/content/aiijc-80x80 (train)/Autoencoder/'+i)
os.makedirs('aiijc-80x80 (validation)', exist_ok = True)
for i in tqdm(random.sample(os.listdir('/content/aiijc-80x80 (train)/Autoencoder'), k = math.floor(len(os.listdir('/content/aiijc-80x80 (train)/Autoencoder'))/30))):
  copyfile('/content/aiijc-80x80 (train)/Autoencoder/' + i, '/content/aiijc-80x80 (validation)/' + i)
  os.remove('/content/aiijc-80x80 (train)/Autoencoder/' + i)

def preprocess_img_init(item, name):
  if name == 'train':
    img = load_img(f'/content/aiijc-80x80 ({name})/Autoencoder/' + item)
    img_array = img_to_array(img)
    img_resized = tf.image.resize(img_array, size = [32, 32], preserve_aspect_ratio = True)
    save_img(f'/content/aiijc-32x32 ({name})/' + item, x = img_resized)
  elif name == 'validation':
    img = load_img(f'/content/aiijc-80x80 ({name})/' + item)
    img_array = img_to_array(img)
    img_resized = tf.image.resize(img_array, size = [32, 32], preserve_aspect_ratio = True)
    save_img(path = f'/content/aiijc-32x32 ({name})/' + item, x = img_resized)   
os.makedirs('aiijc-32x32 (validation)', exist_ok = True) 
os.makedirs('aiijc-32x32 (train)', exist_ok = True)
for i in tqdm(os.listdir('/content/aiijc-80x80 (train)/Autoencoder')):
  preprocess_img_init(item = i, name = 'train')

for i in tqdm(os.listdir('/content/aiijc-80x80 (validation)')):
  preprocess_img_init(item = i, name = 'validation')
os.makedirs('/content/aiijc-64x64 (train)', exist_ok = True)
os.makedirs('/content/aiijc-64x64 (validation)', exist_ok = True)
def preprocess_img(item, name):
  if name =='train':
    img = load_img(f'/content/aiijc-32x32 ({name})/' + item)
    img_array = img_to_array(img)
    img_resized = tf.image.resize(img_array, size = [64, 64], preserve_aspect_ratio = True)
    save_img(f'/content/aiijc-64x64 ({name})/' + item, x = img_resized)
  elif name =='validation':
    img = load_img(f'/content/aiijc-32x32 ({name})/' + item)
    img_array = img_to_array(img)
    img_resized = tf.image.resize(img_array, size = [64, 64], preserve_aspect_ratio = True)
    save_img(path = f'/content/aiijc-64x64 ({name})/' + item, x = img_resized)    
for i in tqdm(os.listdir('/content/aiijc-32x32 (train)')):
  preprocess_img(item = i, name = 'train')
for i in tqdm(os.listdir('/content/aiijc-32x32 (validation)')):
  preprocess_img(item = i, name = 'validation')
from ISR.models import RRDN
from ISR.models import Discriminator
from ISR.models import Cut_VGG19

lr_train_patch_size = 32
layers_to_extract = [5, 9]
scale = 2
hr_train_patch_size = lr_train_patch_size * scale

rrdn  = RRDN(arch_params = {'C':4, 'D':3, 'G':64, 'G0':64, 'T':10, 'x':scale}, patch_size = lr_train_patch_size)
f_ext = Cut_VGG19(patch_size = hr_train_patch_size, layers_to_extract = layers_to_extract)
discr = Discriminator(patch_size = hr_train_patch_size, kernel_size = 3)
from shutil import rmtree

rmtree('aiijc-80x80 (train)')
rmtree('aiijc-80x80 (validation)')
from ISR.train import Trainer
loss_weights = {
  'generator': 0.0,
  'feature_extractor': 0.0833,
  'discriminator': 0.01
}
losses = {
  'generator': 'mae',
  'feature_extractor': 'mse',
  'discriminator': 'binary_crossentropy'
}

log_dirs = {'logs': './logs', 'weights': './weights'}

learning_rate = {'initial_value': 0.0004, 'decay_factor': 0.5, 'decay_frequency': 3}

flatness = {'min': 0.0, 'max': 0.15, 'increase': 0.01, 'increase_frequency': 5}

trainer = Trainer(
    generator = rrdn,
    discriminator = discr,
    feature_extractor = f_ext,
    lr_train_dir = '/content/aiijc-32x32 (train)',
    hr_train_dir = '/content/aiijc-64x64 (train)',
    lr_valid_dir = '/content/aiijc-32x32 (validation)',
    hr_valid_dir = '/content/aiijc-64x64 (validation)',
    loss_weights = loss_weights,
    learning_rate = learning_rate,
    flatness = flatness,
    dataname = 'image_dataset',
    log_dirs = log_dirs,
    weights_generator = '/content/drive/MyDrive/Geoservice analysis/ISR/generator(3).hdf5',
    weights_discriminator = '/content/drive/MyDrive/Geoservice analysis/ISR/discriminator(3).hdf5',
    n_validation = 100,
)
trainer.train(
    epochs = 7,
    steps_per_epoch = 1220,
    batch_size = 38,
    monitored_metrics = {'val_PSNR_Y': 'max'}
)
gen_model = rrdn.model
gen_model.save('/content/drive/MyDrive/Geoservice analysis/ISR/generator(4).hdf5')
discr_model = discr.model
discr_model.save('/content/drive/MyDrive/Geoservice analysis/ISR/discriminator(4).hdf5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12723041117145073, 'fraction_numerical': 0.024592707525213343, 'mean_word_length': 4.191703584373742, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '66260', 'license': 'MIT', 'n_tokens_mistral': 4927, 'n_tokens_neox': 4633, 'n_words': 921, 'path': 'our experiments/GANs_AIIJC_(ISR).ipynb', 'repo_name': 'ngavu2004/trafficsignclassification', 'size': '12890'}