From b38ad802d13cd3904b3988d0fdf026c231eadcd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Wed, 25 May 2022 06:35:16 +0000 Subject: [PATCH 01/11] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20SRGAN=5FID2087=5Ffor?= =?UTF-8?q?=5FTensorflow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.keep diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.keep b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.keep new file mode 100644 index 000000000..e69de29bb -- Gitee From 15bc9b76cda8db1be43ea12622313a3de543bd2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Wed, 25 May 2022 06:36:01 +0000 Subject: [PATCH 02/11] =?UTF-8?q?srgan=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/SRGAN_ID2087_for_Tensorflow/.gitignore | 104 +++++++ .../cv/SRGAN_ID2087_for_Tensorflow/Network.py | 124 +++++++++ .../cv/SRGAN_ID2087_for_Tensorflow/README.md | 145 ++++++++++ .../cv/SRGAN_ID2087_for_Tensorflow/Utils.py | 260 ++++++++++++++++++ .../Utils_model.py | 44 +++ .../cv/SRGAN_ID2087_for_Tensorflow/VGGG.py | 228 +++++++++++++++ .../fusion_switch.cfg | 10 + .../modelarts_entry_acc.py | 66 +++++ .../modelarts_entry_perf.py | 63 +++++ .../cv/SRGAN_ID2087_for_Tensorflow/test.py | 74 +++++ .../cv/SRGAN_ID2087_for_Tensorflow/train.py | 185 +++++++++++++ 11 files changed, 1303 insertions(+) create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.gitignore create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Network.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils_model.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/VGGG.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/fusion_switch.cfg create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test.py create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/train.py diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.gitignore b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.gitignore new file mode 100644 index 000000000..894a44cc0 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/.gitignore @@ -0,0 +1,104 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Network.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Network.py new file mode 100644 index 000000000..34ce3a9f9 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Network.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +#title :Network.py +#description :Architecture file(Generator and Discriminator) +#author :Deepak Birla +#date :2018/10/30 +#usage :from Network import Generator, Discriminator +#python_version :3.5.4 + +# Modules +from keras.layers import Dense +from keras.layers.core import Activation +from keras.layers.normalization import BatchNormalization +from keras.layers.convolutional import UpSampling2D +from keras.layers.core import Flatten +from keras.layers import Input +from keras.layers.convolutional import Conv2D, Conv2DTranspose +from keras.models import Model +from keras.layers.advanced_activations import LeakyReLU, PReLU +from keras.layers import add + +# Residual block +def res_block_gen(model, kernal_size, filters, strides): + + gen = model + + model = Conv2D(filters = filters, kernel_size = kernal_size, strides = strides, padding = "same")(model) + model = BatchNormalization(momentum = 0.5)(model) + # Using Parametric ReLU + model = PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(model) + model = Conv2D(filters = filters, kernel_size = kernal_size, strides = strides, padding = "same")(model) + model = BatchNormalization(momentum = 0.5)(model) + + model = add([gen, model]) + + return model + + +def up_sampling_block(model, kernal_size, filters, strides): + + # In place of Conv2D and UpSampling2D we can also use Conv2DTranspose (Both are used for Deconvolution) + # Even we can have our own function for deconvolution (i.e one made in Utils.py) + #model = Conv2DTranspose(filters = filters, kernel_size = kernal_size, strides = strides, padding = "same")(model) + model = Conv2D(filters = filters, kernel_size = kernal_size, strides = strides, padding = "same")(model) + model = UpSampling2D(size = 2)(model) + model = LeakyReLU(alpha = 0.2)(model) + + return model + + +def discriminator_block(model, filters, kernel_size, strides): + + model = Conv2D(filters = filters, kernel_size = kernel_size, strides = strides, padding = "same")(model) + model = BatchNormalization(momentum = 0.5)(model) + model = LeakyReLU(alpha = 0.2)(model) + + return model + +# Network Architecture is same as given in Paper https://arxiv.org/pdf/1609.04802.pdf +class Generator(object): + + def __init__(self, noise_shape): + + self.noise_shape = noise_shape + + def generator(self): + + gen_input = Input(shape = self.noise_shape) + + model = Conv2D(filters = 64, kernel_size = 9, strides = 1, padding = "same")(gen_input) + model = PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(model) + + gen_model = model + + # Using 16 Residual Blocks + for index in range(16): + model = res_block_gen(model, 3, 64, 1) + + model = Conv2D(filters = 64, kernel_size = 3, strides = 1, padding = "same")(model) + model = BatchNormalization(momentum = 0.5)(model) + model = add([gen_model, model]) + + # Using 2 UpSampling Blocks + for index in range(2): + model = up_sampling_block(model, 3, 256, 1) + + model = Conv2D(filters = 3, kernel_size = 9, strides = 1, padding = "same")(model) + model = Activation('tanh')(model) + + generator_model = Model(inputs = gen_input, outputs = model) + + return generator_model + +# Network Architecture is same as given in Paper https://arxiv.org/pdf/1609.04802.pdf +class Discriminator(object): + + def __init__(self, image_shape): + + self.image_shape = image_shape + + def discriminator(self): + + dis_input = Input(shape = self.image_shape) + + model = Conv2D(filters = 64, kernel_size = 3, strides = 1, padding = "same")(dis_input) + model = LeakyReLU(alpha = 0.2)(model) + + model = discriminator_block(model, 64, 3, 2) + model = discriminator_block(model, 128, 3, 1) + model = discriminator_block(model, 128, 3, 2) + model = discriminator_block(model, 256, 3, 1) + model = discriminator_block(model, 256, 3, 2) + model = discriminator_block(model, 512, 3, 1) + model = discriminator_block(model, 512, 3, 2) + + model = Flatten()(model) + model = Dense(1024)(model) + model = LeakyReLU(alpha = 0.2)(model) + + model = Dense(1)(model) + model = Activation('sigmoid')(model) + + discriminator_model = Model(inputs = dis_input, outputs = model) + + return discriminator_model diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md new file mode 100644 index 000000000..3cb3c88d9 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md @@ -0,0 +1,145 @@ +# Keras-SRGAN +Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network implemented in Keras + +For more about topic check [Single Image Super Resolution Using GANs — Keras](https://medium.com/@birla.deepak26/single-image-super-resolution-using-gans-keras-aca310f33112) + +## Problem Statement: + Enhancing low resolution images by applying deep network with adversarial network (Generative Adversarial Networks) + to produce high resolutions images. + +## Architecture: + +![Basic Architecture](./Architecture_images/architecture.jpg) + +## Generator and Discriminator Network: + +![Network](./Architecture_images/network.jpg) + +## Network Details: + * 16 Residual blocks used. + * PixelShuffler x2: This is feature map upscaling. 2 sub-pixel CNN are used in Generator. + * PRelu(Parameterized Relu): We are using PRelu in place of Relu or LeakyRelu. It introduces learn-able parameter + that makes it possible to adaptively learn the negative part coefficient. + * k3n64s1 this means kernel 3, channels 64 and strides 1. + * Loss Function: We are using Perceptual loss. It comprises of Content(Reconstruction) loss and Adversarial loss. + +## How it Works: + * We process the HR(High Resolution) images to get down-sampled LR(Low Resolution) images. Now we have both HR + and LR images for training data set. + * We pass LR images through Generator which up-samples and gives SR(Super Resolution) images. + * We use a discriminator to distinguish the HR images and back-propagate the GAN loss to train the discriminator + and the generator. + * As a result of this, the generator learns to produce more and more realistic images(High Resolution images) as + it trains. + +## Documentation: +You can find more about this implementation in my post : [Single Image Super Resolution Using GANs — Keras](https://medium.com/@birla.deepak26/single-image-super-resolution-using-gans-keras-aca310f33112) + + +## Requirements: + + You will need the following to run the above: + Python 3.5.4 + tensorflow 1.11.0 + keras 2.2.4 + numpy 1.10.4 + matplotlib, skimage, scipy + + For training: Good GPU, I trained my model on NVIDIA Tesla P100 + +## Data set: + + * Used COCO data set 2017. It is around 18GB having images of different dimensions. + * Used 800 images for training(Very less, You can take more (approx. 350 according to original paper) thousand is you can + collect and have very very good GPU). Preprocessing includes cropping images so that we can have same dimension images. + Images with same width and height are preferred. I used images of size 384 for high resolution. + * After above step you have High Resolution images. Now you have to get Low Resolution images which you can get by down + scaling HR images. I used down scale = 4. So Low resolution image of size 96 we will get. Sample code for this. + +## File Structure: + + Network.py : Contains Generator and Discriminator Network + Utils.py : Contains utilities to process images + Utils_model.py : Contains optimizer and content loss code + train.py : Used for training the model + test.py : To test the model + Simplified : This folder contains code without Agrparse etc. If you hate commandline arguements just dive in here. + There are just two files. Modify according to requirement and start training. + +## Usage: + + Note : Image shape and downscale factor you can set in train.py file.Set according to requirement. + + * Training: + Run below command to train model. Set parameters accordingly. + > python train.py --input_dir='./data/' --output_dir='./output/' --model_save_dir='./model/' --batch_size=64 --epochs=3000 --number_of_images=1000 --train_test_ratio=0.8 + + All Parameters have default values. For mode help on parameters run: + > python train.py -h + + * Testing: + test.py file contains code to test. Testing can be done in two ways using option test_type: + 1. Test Model- Here you can test the model by providing HR images. It will process to get resulting LR images and then will generate SR images. + And then will save output file comprising of all LR, SR and HR images. + Run following command to test model: + > python test.py --input_high_res='./data_hr/' --output_dir='./output/' --model_dir='./model/gen_model3000.h5' --number_of_images=25 --test_type='test_model' + For more help run: + > python test.py -h + + 2. Test LR images- This option directly take LR images and give resulting HR images. + Run following command to get HR images from LR images: + > python test.py --input_low_res='./data_lr/' --output_dir='./output/' --model_dir='./model/gen_model3000.h5' --number_of_images=25 --test_type='test_lr_images' + For more help run: + > python test.py -h + + If you hate commandline arguements please reffer Simplified folder. Modify parameters in file like image_shape, input folder + etc. according to your need and start training. + +## Things's Learned: + + * GAN's sometimes are hard to train. Network can be very deep sometimes, but use of residual blocks make it easier. + * Once you get to learn about Perceptual loss things get easier. Same Perceptual loss can be usefull for Image Style Transfer and Photo Realistic Style Transfer. + * This is one of the problem where i struggled to get data. You need to be carefull while choosing data and also preprossing is little bit tough. + * Better to use images with same width and height. + * Use GPU for training else it will take months to train(even you can run out of memory). + +## Output: + +Below are few results- +![Output 1](./output/gan_generated_image_epoch_1110.png) +![Output 2](./output/gan_generated_image_epoch_2580.png) +![Output 2](./output/gan_generated_image_epoch_770.png) + +More results are in output folder + +## Refrences: + + Paper: + Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network: https://arxiv.org/pdf/1609.04802.pdf + Perceptual Losses for Real-Time Style Transfer and Super-Resolution: https://cs.stanford.edu/people/jcjohns/papers/eccv16/JohnsonECCV16.pdf + + Projects doing the same thing: + https://github.com/MathiasGruber/SRGAN-Keras + https://github.com/titu1994/Super-Resolution-using-Generative-Adversarial-Networks + https://github.com/eriklindernoren/Keras-GAN/tree/master/srgan + https://github.com/brade31919/SRGAN-tensorflow + https://github.com/tensorlayer/srgan + + Help on GANS: + https://github.com/eriklindernoren/Keras-GAN (Various GANS implemented in Keras) + https://github.com/JGuillaumin/SuperResGAN-keras + https://oshearesearch.com/index.php/2016/07/01/mnist-generative-adversarial-model-in-keras/ + + VGG loss help: + https://blog.sicara.com/keras-generative-adversarial-networks-image-deblurring-45e3ab6977b5 + + SubpixelConv2D(Deconvolution) help: + Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network: https://arxiv.org/abs/1609.05158 + https://github.com/twairball/keras-subpixel-conv + + Improved Techniques for Training GANs: + https://arxiv.org/abs/1606.03498 + + + + diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py new file mode 100644 index 000000000..64a40cbdb --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py @@ -0,0 +1,260 @@ +#!/usr/bin/env python +#title :Utils.py +#description :Have helper functions to process images and plot images +#author :Deepak Birla +#date :2018/10/30 +#usage :imported in other files +#python_version :3.5.4 + +from keras.layers import Lambda +import tensorflow as tf + +from skimage import io +import numpy as np +from numpy import array +from numpy.random import randint +import cv2 as cv +from PIL import Image +import os +import sys +import matplotlib.pyplot as plt +plt.switch_backend('agg') + +# Subpixel Conv will upsample from (h, w, c) to (h/r, w/r, c/r^2) +def SubpixelConv2D(input_shape, scale=4): + def subpixel_shape(input_shape): + dims = [input_shape[0],input_shape[1] * scale,input_shape[2] * scale,int(input_shape[3] / (scale ** 2))] + output_shape = tuple(dims) + return output_shape + + def subpixel(x): + return tf.depth_to_space(x, scale) + + return Lambda(subpixel, output_shape=subpixel_shape) + +# Takes list of images and provide HR images in form of numpy array +def hr_images(images): + images_hr = array(images,dtype=object) + return images_hr + +# Takes list of images and provide LR images in form of numpy array +def lr_images(images_real , downscale): + + images = [] + for img in range(len(images_real)): + images.append(cv.resize(images_real[img],(0,0),fx=0.25,fy=0.25,interpolation=cv.INTER_CUBIC)) + images_lr = array(images,dtype=object) + return images_lr + +def normalize(input_data): + + return (input_data.astype(np.float32) - 127.5)/127.5 + +def denormalize(input_data): + input_data = (input_data + 1) * 127.5 + return input_data.astype(np.uint8) + + +def load_path(path): + directories = [] + if os.path.isdir(path): + directories.append(path) + for elem in os.listdir(path): + if os.path.isdir(os.path.join(path,elem)): + directories = directories + load_path(os.path.join(path,elem)) + directories.append(os.path.join(path,elem)) + return directories + +def load_data_from_dirs(dirs, ext): + files = [] + file_names = [] + count = 0 + for d in dirs: + for f in os.listdir(d): + if f.endswith(ext): + image = io.imread(os.path.join(d,f)) + if len(image.shape) > 2: + files.append(image) + file_names.append(os.path.join(d,f)) + count = count + 1 + if count%1000==0 : + print(count) + print('finish load img') + return files + +def load_data(directory, ext): + + files = load_data_from_dirs(load_path(directory), ext) + return files + +def load_training_data(directory, ext, number_of_images = 10000, train_test_ratio = 0.8): + + number_of_train_images = int(number_of_images * train_test_ratio) + files = load_data_from_dirs(load_path(directory), ext) + + if len(files) < number_of_images: + print("Number of image files are less then you specified") + print("Please reduce number of images to %d" % len(files)) + sys.exit() + + test_array = array(files,dtype=object) + if len(test_array[0].shape) < 3: + print("Images are of not same shape") + print("Please provide same shape images") + sys.exit() + + x_train = files[:number_of_train_images] + x_test = files[number_of_train_images:number_of_images] + + x_train_hr = hr_images(x_train) + for i in range(number_of_train_images): + x_train_hr[i] = normalize(x_train_hr[i]) + if i%1000==0: + print(i) + print("x_train_hr finish normalize") + + x_train_lr = lr_images(x_train, 4) + for j in range(number_of_train_images): + x_train_lr[j] = normalize(x_train_lr[j]) + + + x_test_hr = hr_images(x_test) + for k in range(number_of_images-number_of_train_images): + x_test_hr[k] = normalize(x_test_hr[k]) + if k %1000 == 0: + print(k) + print("x_test_hr finish normalize") + + + x_test_lr = lr_images(x_test, 4) + for w in range(number_of_images-number_of_train_images): + x_test_lr[w] = normalize(x_test_lr[w]) + if w %1000 == 0: + print(w) + print("x_test_lr finish normalize") + + + return x_train_lr, x_train_hr, x_test_lr, x_test_hr + + +def load_test_data_for_model(directory, ext, number_of_images = 100): + + files = load_data_from_dirs(load_path(directory), ext) + + if len(files) < number_of_images: + print("Number of image files are less then you specified") + print("Please reduce number of images to %d" % len(files)) + sys.exit() + + x_test_hr = hr_images(files) + x_test_hr = normalize(x_test_hr) + + x_test_lr = lr_images(files, 4) + x_test_lr = normalize(x_test_lr) + + return x_test_lr, x_test_hr + +def load_test_data(directory, ext, number_of_images = 100): + + files = load_data_from_dirs(load_path(directory), ext) + + if len(files) < number_of_images: + print("Number of image files are less then you specified") + print("Please reduce number of images to %d" % len(files)) + sys.exit() + + x_test_lr = lr_images(files, 4) + x_test_lr = normalize(x_test_lr) + + return x_test_lr + +# While training save generated image(in form LR, SR, HR) +# Save only one image as sample +def plot_generated_images(output_dir, epoch, generator, x_test_hr, x_test_lr , dim=(1, 3), figsize=(15, 5)): + + examples = x_test_hr.shape[0] + print(examples) + value = randint(0, examples) + image_batch_hr = denormalize(x_test_hr) + image_batch_lr = x_test_lr + gen_img = generator.predict(image_batch_lr,batch_size=16) + generated_image = denormalize(gen_img) + image_batch_lr = denormalize(image_batch_lr) + + plt.figure(figsize=figsize) + + plt.subplot(dim[0], dim[1], 1) + plt.imshow(image_batch_lr[value], interpolation='nearest') + plt.axis('off') + + plt.subplot(dim[0], dim[1], 2) + plt.imshow(generated_image[value], interpolation='nearest') + plt.axis('off') + + plt.subplot(dim[0], dim[1], 3) + plt.imshow(image_batch_hr[value], interpolation='nearest') + plt.axis('off') + + plt.tight_layout() + plt.savefig(output_dir + 'generated_image_%d.png' % epoch) + + #plt.show() + +# Plots and save generated images(in form LR, SR, HR) from model to test the model +# Save output for all images given for testing +def plot_test_generated_images_for_model(output_dir, generator, x_test_hr, x_test_lr , dim=(1, 3), figsize=(15, 5)): + + examples = x_test_hr.shape[0] + image_batch_hr = denormalize(x_test_hr) + image_batch_lr = x_test_lr + gen_img = generator.predict(image_batch_lr) + generated_image = denormalize(gen_img) + image_batch_lr = denormalize(image_batch_lr) + + for index in range(examples): + + plt.figure(figsize=figsize) + + plt.subplot(dim[0], dim[1], 1) + plt.imshow(image_batch_lr[index], interpolation='nearest') + plt.axis('off') + + plt.subplot(dim[0], dim[1], 2) + plt.imshow(generated_image[index], interpolation='nearest') + plt.axis('off') + + plt.subplot(dim[0], dim[1], 3) + plt.imshow(image_batch_hr[index], interpolation='nearest') + plt.axis('off') + + plt.tight_layout() + plt.savefig(output_dir + 'test_generated_image_%d.png' % index) + + #plt.show() + +# Takes LR images and save respective HR images +def plot_test_generated_images(output_dir, generator, x_test_lr, figsize=(5, 5)): + + examples = x_test_lr.shape[0] + #image_batch_lr = denormalize(x_test_lr) + image_batch_lr = x_test_lr + print(image_batch_lr.shape) + gen_img = generator.predict(image_batch_lr) + generated_image = denormalize(gen_img) + print(generated_image.shape) + for index in range(examples): + + #plt.figure(figsize=figsize) + + plt.imshow(generated_image[index], interpolation='nearest') + plt.axis('off') + + plt.tight_layout() + plt.savefig(output_dir + 'high_res_result_image_%d.png' % index) + + #plt.show() + + + + + diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils_model.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils_model.py new file mode 100644 index 000000000..95811a21e --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils_model.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +#title :Utils_model.py +#description :Have functions to get optimizer and loss +#author :Deepak Birla +#date :2018/10/30 +#usage :imported in other files +#python_version :3.5.4 + +from keras.applications.vgg19 import VGG19 +import keras.backend as K +from keras.models import Model +from keras.optimizers import Adam +import tensorflow as tf +from npu_bridge.npu_init import * +class VGG_LOSS(object): + + def __init__(self, image_shape): + + self.image_shape = image_shape + + # computes VGG loss or content loss + def vgg_loss(self, y_true, y_pred): + + vgg19 = VGG19(include_top=False, weights='imagenet', input_shape=self.image_shape) + vgg19.trainable = False + # Make trainable as False + for l in vgg19.layers: + l.trainable = False + model = Model(inputs=vgg19.input, outputs=vgg19.get_layer('block5_conv4').output) + model.trainable = False + + return K.mean(K.square(model(y_true) - model(y_pred))) + +def get_optimizer(): + # adam = Adam(lr=1E-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08) + opt = tf.train.AdamOptimizer(learning_rate=1E-4, beta1=0.9, beta2=0.999, epsilon=1e-08) + + # loss_scale_manager = FixedLossScaleManager(loss_scale=2**10) + loss_scale_manager = ExponentialUpdateLossScaleManager(init_loss_scale=2 ** 32, incr_every_n_steps=500, + decr_every_n_nan_or_inf=2, decr_ratio=0.5) + opt = NPULossScaleOptimizer(opt, loss_scale_manager) + + return opt + diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/VGGG.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/VGGG.py new file mode 100644 index 000000000..c57a6ed9d --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/VGGG.py @@ -0,0 +1,228 @@ +"""VGG19 model for Keras. + +# Reference + +- [Very Deep Convolutional Networks for Large-Scale Image Recognition]( + https://arxiv.org/abs/1409.1556) (ICLR 2015) + +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os + +from . import get_submodules_from_kwargs +from . import imagenet_utils +from .imagenet_utils import decode_predictions +from .imagenet_utils import _obtain_input_shape + +preprocess_input = imagenet_utils.preprocess_input + +WEIGHTS_PATH = ('https://github.com/fchollet/deep-learning-models/' + 'releases/download/v0.1/' + 'vgg19_weights_tf_dim_ordering_tf_kernels.h5') +WEIGHTS_PATH_NO_TOP = ('https://github.com/fchollet/deep-learning-models/' + 'releases/download/v0.1/' + 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5') + + +def VGG19(include_top=True, + weights='imagenet', + input_tensor=None, + input_shape=None, + pooling=None, + classes=1000, + **kwargs): + """Instantiates the VGG19 architecture. + + 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 + include_top: whether to include the 3 fully-connected + layers 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, + and width and height should be no smaller than 32. + 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 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. + + # Returns + A Keras model instance. + + # Raises + ValueError: in case of invalid argument for `weights`, + or invalid input shape. + """ + backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs) + + 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') + # Determine proper input shape + input_shape = _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 + # Block 1 + x = layers.Conv2D(64, (3, 3), + activation='relu', + padding='same', + name='block1_conv1')(img_input) + x = layers.Conv2D(64, (3, 3), + activation='relu', + padding='same', + name='block1_conv2')(x) + x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) + + # Block 2 + x = layers.Conv2D(128, (3, 3), + activation='relu', + padding='same', + name='block2_conv1')(x) + x = layers.Conv2D(128, (3, 3), + activation='relu', + padding='same', + name='block2_conv2')(x) + x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) + + # Block 3 + x = layers.Conv2D(256, (3, 3), + activation='relu', + padding='same', + name='block3_conv1')(x) + x = layers.Conv2D(256, (3, 3), + activation='relu', + padding='same', + name='block3_conv2')(x) + x = layers.Conv2D(256, (3, 3), + activation='relu', + padding='same', + name='block3_conv3')(x) + x = layers.Conv2D(256, (3, 3), + activation='relu', + padding='same', + name='block3_conv4')(x) + x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) + + # Block 4 + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block4_conv1')(x) + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block4_conv2')(x) + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block4_conv3')(x) + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block4_conv4')(x) + x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) + + # Block 5 + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block5_conv1')(x) + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block5_conv2')(x) + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block5_conv3')(x) + x = layers.Conv2D(512, (3, 3), + activation='relu', + padding='same', + name='block5_conv4')(x) + x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) + + if include_top: + # Classification block + x = layers.Flatten(name='flatten')(x) + x = layers.Dense(4096, activation='relu', name='fc1')(x) + x = layers.Dense(4096, activation='relu', name='fc2')(x) + x = layers.Dense(classes, activation='softmax', 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 = keras_utils.get_source_inputs(input_tensor) + else: + inputs = img_input + # Create model. + model = models.Model(inputs, x, name='vgg19') + + # Load weights. + if weights == 'imagenet': + if include_top: + weights_path = keras_utils.get_file( + 'vgg19_weights_tf_dim_ordering_tf_kernels.h5', + WEIGHTS_PATH, + cache_subdir='models', + file_hash='cbe5617147190e668d6c5d5026f83318') + else: + weights_path = keras_utils.get_file( + 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', + WEIGHTS_PATH_NO_TOP, + cache_subdir='models', + file_hash='253f8cb515780f3b799900260a226db6') + model.load_weights(weights_path) + if backend.backend() == 'theano': + keras_utils.convert_all_kernels_in_model(model) + elif weights is not None: + model.load_weights(weights) + + return model diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/fusion_switch.cfg b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/fusion_switch.cfg new file mode 100644 index 000000000..f21fffae8 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/fusion_switch.cfg @@ -0,0 +1,10 @@ +{ +"Switch":{ +"GraphFusion":{ +"ALL":"off" +}, +"UBFusion":{ +"ALL":"off" +} +} +} \ No newline at end of file diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py new file mode 100644 index 000000000..8350b7a02 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py @@ -0,0 +1,66 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import argparse +import sys + + +#print(os.system("pip install keras==2.2.4")) + +# 解析输入参数data_url +parser = argparse.ArgumentParser() +parser.add_argument("--data_url", type=str, default="/home/ma-user/modelarts/inputs/data_url_0") +parser.add_argument("--train_url", type=str, default="/home/ma-user/modelarts/outputs/train_url_0/") +config = parser.parse_args() + +print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) +code_dir = sys.path[0] +os.chdir(code_dir) +print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) + +print("[CANN-Modelzoo] before train - list my run files:") +os.system("ls -al /usr/local/Ascend/ascend-toolkit/") + +print("[CANN-Modelzoo] before train - list my dataset files:") +os.system("ls -al %s" % config.data_url) + +print("[CANN-Modelzoo] start run train shell") +# 设置sh文件格式为linux可执行 +os.system("dos2unix ./test/*") + +# 执行train_full_1p.sh或者train_performance_1p.sh,需要用户自己指定 +# full和performance的差异,performance只需要执行很少的step,控制在15分钟以内,主要关注性能FPS +os.system("bash ./test/train_full_1p.sh --data_path=%s --output_path=%s " % (config.data_url, config.train_url)) + +print("[CANN-Modelzoo] finish run train shell") + +# 将当前执行目录所有文件拷贝到obs的output进行备份 +print("[CANN-Modelzoo] after train - list my output files:") +os.system("cp -r %s %s " % (code_dir, config.train_url)) +os.system("ls -al %s" % config.train_url) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py new file mode 100644 index 000000000..14384e227 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py @@ -0,0 +1,63 @@ +# Copyright 2017 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import argparse +import sys + +# 解析输入参数data_url +parser = argparse.ArgumentParser() +parser.add_argument("--data_url", type=str, default="/home/ma-user/modelarts/inputs/data_url_0") +parser.add_argument("--train_url", type=str, default="/home/ma-user/modelarts/outputs/train_url_0/") +config = parser.parse_args() + +print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) +code_dir = sys.path[0] +os.chdir(code_dir) +print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) + +print("[CANN-Modelzoo] before train - list my run files:") +os.system("ls -al /usr/local/Ascend/ascend-toolkit/") + +print("[CANN-Modelzoo] before train - list my dataset files:") +os.system("ls -al %s" % config.data_url) + +print("[CANN-Modelzoo] start run train shell") +# 设置sh文件格式为linux可执行 +os.system("dos2unix ./test/*") + +# 执行train_full_1p.sh或者train_performance_1p.sh,需要用户自己指定 +# full和performance的差异,performance只需要执行很少的step,控制在15分钟以内,主要关注性能FPS +os.system("bash ./test/train_performance_1p.sh --data_path=%s --output_path=%s " % (config.data_url, config.train_url)) + +print("[CANN-Modelzoo] finish run train shell") + +# 将当前执行目录所有文件拷贝到obs的output进行备份 +print("[CANN-Modelzoo] after train - list my output files:") +os.system("cp -r %s %s " % (code_dir, config.train_url)) +os.system("ls -al %s" % config.train_url) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test.py new file mode 100644 index 000000000..a4c56e421 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +#title :test.py +#description :to test the model +#author :Deepak Birla +#date :2018/10/30 +#usage :python test.py --options +#python_version :3.5.4 + +from keras.models import Model +import matplotlib.pyplot as plt +import tensorflow as tf +import skimage.transform +from skimage import data, io, filters +import numpy as np +from numpy import array +import os +from keras.models import load_model +from scipy.misc import imresize +import argparse + +import Utils, Utils_model +from Utils_model import VGG_LOSS + +image_shape = (192,192,3) + +def test_model(input_hig_res, model, number_of_images, output_dir): + + x_test_lr, x_test_hr = Utils.load_test_data_for_model(input_hig_res, 'jpg', number_of_images) + Utils.plot_test_generated_images_for_model(output_dir, model, x_test_hr, x_test_lr) + +def test_model_for_lr_images(input_low_res, model, number_of_images, output_dir): + + x_test_lr = Utils.load_test_data(input_low_res, 'jpg', number_of_images) + Utils.plot_test_generated_images(output_dir, model, x_test_lr) + +if __name__== "__main__": + + parser = argparse.ArgumentParser() + + parser.add_argument('-ihr', '--input_hig_res', action='store', dest='input_hig_res', default='./data/' , + help='Path for input images Hig resolution') + + parser.add_argument('-ilr', '--input_low_res', action='store', dest='input_low_res', default='./data_lr/' , + help='Path for input images Low resolution') + + parser.add_argument('-o', '--output_dir', action='store', dest='output_dir', default='./output/' , + help='Path for Output images') + + parser.add_argument('-m', '--model_dir', action='store', dest='model_dir', default='./model/gen_model3000.h5' , + help='Path for model') + + parser.add_argument('-n', '--number_of_images', action='store', dest='number_of_images', default=25 , + help='Number of Images', type=int) + + parser.add_argument('-t', '--test_type', action='store', dest='test_type', default='test_model', + help='Option to test model output or to test low resolution image') + + values = parser.parse_args() + + loss = VGG_LOSS(image_shape) + model = load_model(values.model_dir , custom_objects={'vgg_loss': loss.vgg_loss}) + + if values.test_type == 'test_model': + test_model(values.input_hig_res, model, values.number_of_images, values.output_dir) + + elif values.test_type == 'test_lr_images': + test_model_for_lr_images(values.input_low_res, model, values.number_of_images, values.output_dir) + + else: + print("No such option") + + + + diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/train.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/train.py new file mode 100644 index 000000000..9bd0708bc --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/train.py @@ -0,0 +1,185 @@ +#!/usr/bin/env python +#title :train.py +#description :to train the model +#author :Deepak Birla +#date :2018/10/30 +#usage :python train.py --options +#python_version :3.5.4 +# encoding: utf-8 + +import os +#print(os.system("pip install keras==2.2.4")) + +from Network import Generator, Discriminator +import Utils_model, Utils +from Utils_model import VGG_LOSS +import tensorflow as tf +import keras as keras +from keras import backend as K +from keras.models import Model +from keras.layers import Input +from tqdm import tqdm +import numpy as np +import argparse + +from npu_bridge.npu_init import * +sess_config=tf.ConfigProto() +custom_op =sess_config.graph_options.rewrite_options.custom_optimizers.add() +custom_op.name = "NpuOptimizer" +sess_config.graph_options.rewrite_options.remapping=RewriterConfig.OFF +sess_config.graph_options.rewrite_options.memory_optimization=RewriterConfig.OFF + +# custom_op.parameter_map["precision_mode"].s=tf.compat.as_bytes("force_fp32") +#custom_op.parameter_map["precision_mode"].s=tf.compat.as_bytes("allow_mix_precision") + + + +#import precision_tool.tf_config as npu_tf_config +#sess_config = npu_tf_config.session_dump_config(sess_config, action='overflow') +#npu_keras_sess = set_keras_session_npu_config(config=sess_config) + +#b +#import moxing as mox +#import precision_tool.config as CONFIG + + +# +#custom_op.parameter_map["fusion_switch_file"].s=tf.compat.as_bytes("/home/ma-user/modelarts/user-job-dir/code/fusion_switch.cfg") +sess=tf.Session(config=sess_config) +K.set_session(sess) + +np.random.seed(10) +# Better to use downscale factor as 4 +downscale_factor = 4 +# Remember to change image shape if you are having different size of images +image_shape = (192,192,3) + + + +# Combined network +def get_gan_network(discriminator, shape, generator, optimizer, vgg_loss): + discriminator.trainable = False + gan_input = Input(shape=shape) + x = generator(gan_input) + gan_output = discriminator(x) + gan = Model(inputs=gan_input, outputs=[x,gan_output]) + gan.compile(loss=[vgg_loss, "binary_crossentropy"], + loss_weights=[1., 1e-3], + optimizer=optimizer) + + return gan + +# default values for all parameters are given, if want defferent values you can give via commandline +# for more info use $python train.py -h +def train(epochs, batch_size, input_dir, output_dir, model_save_dir, number_of_images, train_test_ratio): + + x_train_lr, x_train_hr, x_test_lr, x_test_hr = Utils.load_training_data(input_dir, '.jpg', number_of_images, train_test_ratio) + + loss = VGG_LOSS(image_shape) + + batch_count = int(x_train_hr.shape[0] / batch_size) + shape = (image_shape[0]//downscale_factor, image_shape[1]//downscale_factor, image_shape[2]) + + generator = Generator(shape).generator() + discriminator = Discriminator(image_shape).discriminator() + + optimizer = Utils_model.get_optimizer() + + + generator.compile(loss=loss.vgg_loss, optimizer=optimizer) + discriminator.compile(loss="binary_crossentropy", optimizer=optimizer) + + gan = get_gan_network(discriminator, shape, generator, optimizer, loss.vgg_loss) + + loss_file = open(model_save_dir + 'losses.txt' , 'w+') + loss_file.close() + print("huanjing") + #print (os.environ) + #print (os.system("pip3 list")) + #print (custom_op.parameter_map) + + + for e in range(1, epochs+1): + print ('-'*15, 'Epoch %d' % e, '-'*15) + for _ in tqdm(range(batch_count)): + + rand_nums = np.random.randint(0, x_train_hr.shape[0], size=batch_size) + + image_batch_hr = x_train_hr[rand_nums] + image_batch_lr = x_train_lr[rand_nums] + generated_images_sr = generator.predict(image_batch_lr) + + real_data_Y = np.ones(batch_size) - np.random.random_sample(batch_size)*0.2 + fake_data_Y = np.random.random_sample(batch_size)*0.2 + + discriminator.trainable = True + + d_loss_real = discriminator.train_on_batch(image_batch_hr, real_data_Y) + d_loss_fake = discriminator.train_on_batch(generated_images_sr, fake_data_Y) + discriminator_loss = 0.5 * np.add(d_loss_fake, d_loss_real) + + rand_nums = np.random.randint(0, x_train_hr.shape[0], size=batch_size) + + image_batch_hr = x_train_hr[rand_nums] + image_batch_lr = x_train_lr[rand_nums] + + gan_Y = np.ones(batch_size) - np.random.random_sample(batch_size)*0.2 + discriminator.trainable = False + gan_loss = gan.train_on_batch(image_batch_lr, [image_batch_hr,gan_Y]) + + + print("discriminator_loss : %f" % discriminator_loss) + print("gan_loss :", gan_loss) + gan_loss = str(gan_loss) + + loss_file = open(model_save_dir + 'losses.txt' , 'a') + loss_file.write('epoch%d : gan_loss = %s ; discriminator_loss = %f\n' %(e, gan_loss, discriminator_loss) ) + loss_file.close() + + if e % 2 == 0: + + Utils.plot_generated_images(output_dir, e, generator, x_test_hr, x_test_lr) + if e % 500 == 0: + generator.save(model_save_dir + 'gen_model%d.h5' % e) + discriminator.save(model_save_dir + 'dis_model%d.h5' % e) + + +if __name__== "__main__": + + parser = argparse.ArgumentParser() + + parser.add_argument('-i', '--input_dir', action='store', dest='input_dir', default='./tra/' , + help='Path for input images') + + parser.add_argument('-o', '--output_dir', action='store', dest='output_dir', default='./output/' , + help='Path for Output images') + + parser.add_argument('-m', '--model_save_dir', action='store', dest='model_save_dir', default='./model/' , + help='Path for model') + + parser.add_argument('-b', '--batch_size', action='store', dest='batch_size', default=16, + help='Batch Size', type=int) + + parser.add_argument('-e', '--epochs', action='store', dest='epochs', default=800 , + help='number of iteratios for trainig', type=int) + + parser.add_argument('-n', '--number_of_images', action='store', dest='number_of_images', default=10000 , + help='Number of Images', type= int) + + parser.add_argument('-r', '--train_test_ratio', action='store', dest='train_test_ratio', default=0.8 , + help='Ratio of train and test Images', type=float) + + values = parser.parse_args() + #print("shuru:"+values) + train(values.epochs, values.batch_size, values.input_dir, values.output_dir, values.model_save_dir, values.number_of_images, values.train_test_ratio) + + + +#FLAGS.obs_dir="/home/ma-user/modelarts/outputs/train_url_0/" +#obs_overflow_dir = os.path.join(FLAGS.obs_dir, 'overflow') +#if not mox.file.exists(obs_overflow_dir): +# mox.file.make_dirs(obs_overflow_dir) +# files = os.listdir(CONFIG.ROOT_DIR) +#mox.file.copy_parallel(src_url=CONFIG.ROOT_DIR, dst_url=obs_overflow_dir) + +sess.close() \ No newline at end of file -- Gitee From 8f30724039cf8ab8e0c2a83aacca5a5dffbb46ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Wed, 25 May 2022 06:36:52 +0000 Subject: [PATCH 03/11] =?UTF-8?q?srgan=E7=9A=84=E8=AE=AD=E7=BB=83=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/train_full_1p.sh | 225 ++++++++++++++++++ .../test/train_performance_1p.sh | 203 ++++++++++++++++ 2 files changed, 428 insertions(+) create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh create mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh new file mode 100644 index 000000000..95eb1ba9c --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh @@ -0,0 +1,225 @@ +#!/bin/bash + +########################################################## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +########################################################## +# shell脚本所在路径 +cur_path=`echo $(cd $(dirname $0);pwd)` + +# 判断当前shell是否是performance +perf_flag=`echo $0 | grep performance | wc -l` + +# 当前执行网络的名称 +Network=`echo $(cd $(dirname $0);pwd) | awk -F"/" '{print $(NF-1)}'` + +export RANK_SIZE=1 +export RANK_ID=0 +export JOB_ID=10087 + +# 路径参数初始化 +data_path="" +output_path="" + +# 帮助信息,不需要修改 +if [[ $1 == --help || $1 == -h ]];then + echo"usage:./train_performance_1P.sh " + echo " " + echo "parameter explain: + --data_path # dataset of training + --output_path # output of training + --train_steps # max_step for training + --train_epochs # max_epoch for training + --batch_size # batch size + -h/--help show help message + " + exit 1 +fi + +# 参数校验,不需要修改 +for para in $* +do + if [[ $para == --data_path* ]];then + data_path=`echo ${para#*=}` + elif [[ $para == --output_path* ]];then + output_path=`echo ${para#*=}` + elif [[ $para == --train_steps* ]];then + train_steps=`echo ${para#*=}` + elif [[ $para == --train_epochs* ]];then + train_epochs=`echo ${para#*=}` + elif [[ $para == --batch_size* ]];then + batch_size=`echo ${para#*=}` + fi +done + +# 校验是否传入data_path,不需要修改 +if [[ $data_path == "" ]];then + echo "[Error] para \"data_path\" must be config" + exit 1 +fi + +# 校验是否传入output_path,不需要修改 +if [[ $output_path == "" ]];then + output_path="./test/output/${ASCEND_DEVICE_ID}" +fi + +# 设置打屏日志文件名,请保留,文件名为${print_log} +print_log="./test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log" +modelarts_flag=${MODELARTS_MODEL_PATH} +if [ x"${modelarts_flag}" != x ]; +then + echo "running without etp..." + print_log_name=`ls /home/ma-user/modelarts/log/ | grep proc-rank` + print_log="/home/ma-user/modelarts/log/${print_log_name}" +fi +echo "### get your log here : ${print_log}" + +CaseName="" +function get_casename() +{ + if [ x"${perf_flag}" = x1 ]; + then + CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'perf' + else + CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'acc' + fi +} + +# 跳转到code目录 +cd ${cur_path}/../ +rm -rf ./test/output/${ASCEND_DEVICE_ID} +mkdir -p ./test/output/${ASCEND_DEVICE_ID} + +# 训练开始时间记录,不需要修改 +start_time=$(date +%s) +########################################################## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +########################################################## + +#========================================================= +#========================================================= +#========训练执行命令,需要根据您的网络进行修改============== +#========================================================= +#========================================================= +# 基础参数,需要模型审视修改 +# 您的训练数据集在${data_path}路径下,请直接使用这个变量获取 +# 您的训练输出目录在${output_path}路径下,请直接使用这个变量获取 +# 您的其他基础参数,可以自定义增加,但是batch_size请保留,并且设置正确的值 +pwd + +python -m pip install --upgrade pip + +pip install keras==2.2.4 + +cp -r ./mat/* /usr/local/Ascend/ascend-toolkit/5.1.RC2.alpha001/arm64-linux/opp/op_impl/built-in/ai_core/tbe/impl/ + + +pip3 list | grep -i keras + +ls ./h5 +mkdir -p ~/.keras/models/ +#ls ~ +cp -r ./h5/* ~/.keras/models/ +ls ~/.keras/models/ + +#export ASCEND_GLOBAL_LOG_LEVEL=0 +#export ASCEND_SLOG_PRINT_TO_STDOUT=1 + +export LD_PRELOAD=/home/ma-user/miniconda3/envs/TensorFlow-1.15-arm/bin/../lib/libgomp.so.1:$LD_PRELOAD + +batch_size=16 + +if [ x"${modelarts_flag}" != x ]; +then + + + python3.7 ./train.py \ + --input_dir=${data_path} \ + --output_dir=${output_path} \ + --batch_size=16 \ + --epochs=20 \ + --number_of_images=400 \ + --train_test_ratio=0.8 + +else + + + python3.7 ./train.py \ + --input_dir=${data_path} \ + --output_dir=${output_path} \ + --batch_size=16 \ + --epochs=20 \ + --number_of_images=400 \ + --train_test_ratio=0.8 + > ${print_log} +fi + + + +# 性能相关数据计算 +StepTime=`grep "sec/step :" ${print_log} | tail -n 10 | awk '{print $NF}' | awk '{sum+=$1} END {print sum/NR}'` +FPS=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'/'${StepTime}'}'` + +# 精度相关数据计算 +train_accuracy=`grep "Final Accuracy accuracy" ${print_log} | awk '{print $NF}'` +# 提取所有loss打印信息 +grep "loss :" ${print_log} | awk -F ":" '{print $4}' | awk -F "-" '{print $1}' > ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt + + +########################################################### +#########后面的所有内容请不要修改########################### +#########后面的所有内容请不要修改########################### +#########后面的所有内容请不要修改########################### +########################################################### + +# 判断本次执行是否正确使用Ascend NPU +use_npu_flag=`grep "The model has been compiled on the Ascend AI processor" ${print_log} | wc -l` +if [ x"${use_npu_flag}" == x0 ]; +then + echo "------------------ ERROR NOTICE START ------------------" + echo "ERROR, your task haven't used Ascend NPU, please check your npu Migration." + echo "------------------ ERROR NOTICE END------------------" +else + echo "------------------ INFO NOTICE START------------------" + echo "INFO, your task have used Ascend NPU, please check your result." + echo "------------------ INFO NOTICE END------------------" +fi + +# 获取最终的casename,请保留,case文件名为${CaseName} +get_casename + +# 重命名loss文件 +if [ -f ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ]; +then + mv ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ./test/output/${ASCEND_DEVICE_ID}/${CaseName}_loss.txt +fi + +# 训练端到端耗时 +end_time=$(date +%s) +e2e_time=$(( $end_time - $start_time )) + +echo "------------------ Final result ------------------" +# 输出性能FPS/单step耗时/端到端耗时 +echo "Final Performance images/sec : $FPS" +echo "Final Performance sec/step : $StepTime" +echo "E2E Training Duration sec : $e2e_time" + +# 输出训练精度 +echo "Final Train Accuracy : ${train_accuracy}" + +# 最后一个迭代loss值,不需要修改 +ActualLoss=(`awk 'END {print $NF}' $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}_loss.txt`) + +#关键信息打印到${CaseName}.log中,不需要修改 +echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${batch_size}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DeviceType = `uname -m`" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualFPS = ${FPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainingTime = ${StepTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log \ No newline at end of file diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh new file mode 100644 index 000000000..8e39d7a68 --- /dev/null +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh @@ -0,0 +1,203 @@ +#!/bin/bash + +########################################################## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +########################################################## +# shell脚本所在路径 +cur_path=`echo $(cd $(dirname $0);pwd)` + +# 判断当前shell是否是performance +perf_flag=`echo $0 | grep performance | wc -l` + +# 当前执行网络的名称 +Network=`echo $(cd $(dirname $0);pwd) | awk -F"/" '{print $(NF-1)}'` + +export RANK_SIZE=1 +export RANK_ID=0 +export JOB_ID=10087 + +# 路径参数初始化 +data_path="" +output_path="" + +# 帮助信息,不需要修改 +if [[ $1 == --help || $1 == -h ]];then + echo"usage:./train_performance_1P.sh " + echo " " + echo "parameter explain: + --data_path # dataset of training + --output_path # output of training + --train_steps # max_step for training + --train_epochs # max_epoch for training + --batch_size # batch size + -h/--help show help message + " + exit 1 +fi + +# 参数校验,不需要修改 +for para in $* +do + if [[ $para == --data_path* ]];then + data_path=`echo ${para#*=}` + elif [[ $para == --output_path* ]];then + output_path=`echo ${para#*=}` + elif [[ $para == --train_steps* ]];then + train_steps=`echo ${para#*=}` + elif [[ $para == --train_epochs* ]];then + train_epochs=`echo ${para#*=}` + elif [[ $para == --batch_size* ]];then + batch_size=`echo ${para#*=}` + fi +done + +# 校验是否传入data_path,不需要修改 +if [[ $data_path == "" ]];then + echo "[Error] para \"data_path\" must be config" + exit 1 +fi + +# 校验是否传入output_path,不需要修改 +if [[ $output_path == "" ]];then + output_path="./test/output/${ASCEND_DEVICE_ID}" +fi + +# 设置打屏日志文件名,请保留,文件名为${print_log} +print_log="./test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log" +modelarts_flag=${MODELARTS_MODEL_PATH} +if [ x"${modelarts_flag}" != x ]; +then + echo "running with modelarts..." + print_log_name=`ls /home/ma-user/modelarts/log/ | grep proc-rank` + print_log="/home/ma-user/modelarts/log/${print_log_name}" +fi +echo "### get your log here : ${print_log}" + +CaseName="" +function get_casename() +{ + if [ x"${perf_flag}" = x1 ]; + then + CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'perf' + else + CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'acc' + fi +} + +# 跳转到code目录 +cd ${cur_path}/../ +rm -rf ./test/output/${ASCEND_DEVICE_ID} +mkdir -p ./test/output/${ASCEND_DEVICE_ID} + +# 训练开始时间记录,不需要修改 +start_time=$(date +%s) +########################################################## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +#########第3行 至 100行,请一定不要、不要、不要修改########## +########################################################## + +#========================================================= +#========================================================= +#========训练执行命令,需要根据您的网络进行修改============== +#========================================================= +#========================================================= +# 基础参数,需要模型审视修改 +# 您的训练数据集在${data_path}路径下,请直接使用这个变量获取 +# 您的训练输出目录在${output_path}路径下,请直接使用这个变量获取 +# 您的其他基础参数,可以自定义增加,但是batch_size请保留,并且设置正确的值 +train_epochs=800 +train_steps=100 +batch_size=32 + +if [ x"${modelarts_flag}" != x ]; +then + + + python3.7 ./train.py \ + --input_dir=${data_path} \ + --output_dir=${output_path} \ + --batch_size=32 \ + --epochs=800 \ + --number_of_images=20000 \ + --train_test_ratio=0.8 + +else + + + python3.7 ./train.py \ + --input_dir=${data_path} \ + --output_dir=${output_path} \ + --batch_size=32 \ + --epochs=800 \ + --number_of_images=20000 \ + --train_test_ratio=0.8 + > ${print_log} +fi + +# 性能相关数据计算 +StepTime=`grep "sec/step :" ${print_log} | tail -n 10 | awk '{print $NF}' | awk '{sum+=$1} END {print sum/NR}'` +FPS=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'/'${StepTime}'}'` + +# 精度相关数据计算 +train_accuracy=`grep "Final Accuracy accuracy" ${print_log} | awk '{print $NF}'` +# 提取所有loss打印信息 +grep "loss :" ${print_log} | awk -F ":" '{print $4}' | awk -F "-" '{print $1}' > ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt + + +########################################################### +#########后面的所有内容请不要修改########################### +#########后面的所有内容请不要修改########################### +#########后面的所有内容请不要修改########################### +########################################################### + +# 判断本次执行是否正确使用Ascend NPU +use_npu_flag=`grep "The model has been compiled on the Ascend AI processor" ${print_log} | wc -l` +if [ x"${use_npu_flag}" == x0 ]; +then + echo "------------------ ERROR NOTICE START ------------------" + echo "ERROR, your task haven't used Ascend NPU, please check your npu Migration." + echo "------------------ ERROR NOTICE END------------------" +else + echo "------------------ INFO NOTICE START------------------" + echo "INFO, your task have used Ascend NPU, please check your result." + echo "------------------ INFO NOTICE END------------------" +fi + +# 获取最终的casename,请保留,case文件名为${CaseName} +get_casename + +# 重命名loss文件 +if [ -f ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ]; +then + mv ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ./test/output/${ASCEND_DEVICE_ID}/${CaseName}_loss.txt +fi + +# 训练端到端耗时 +end_time=$(date +%s) +e2e_time=$(( $end_time - $start_time )) + +echo "------------------ Final result ------------------" +# 输出性能FPS/单step耗时/端到端耗时 +echo "Final Performance images/sec : $FPS" +echo "Final Performance sec/step : $StepTime" +echo "E2E Training Duration sec : $e2e_time" + +# 输出训练精度 +echo "Final Train Accuracy : ${train_accuracy}" + +# 最后一个迭代loss值,不需要修改 +ActualLoss=(`awk 'END {print $NF}' $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}_loss.txt`) + +#关键信息打印到${CaseName}.log中,不需要修改 +echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "BatchSize = ${batch_size}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "DeviceType = `uname -m`" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualFPS = ${FPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "TrainingTime = ${StepTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log +echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log \ No newline at end of file -- Gitee From ae3fd09ab7d1aa65992c32b68a903c869588b23f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Wed, 25 May 2022 06:59:56 +0000 Subject: [PATCH 04/11] update TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md. --- .../cv/SRGAN_ID2087_for_Tensorflow/README.md | 260 ++++++++---------- 1 file changed, 115 insertions(+), 145 deletions(-) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md index 3cb3c88d9..f133b77d3 100644 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md @@ -1,145 +1,115 @@ -# Keras-SRGAN -Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network implemented in Keras - -For more about topic check [Single Image Super Resolution Using GANs — Keras](https://medium.com/@birla.deepak26/single-image-super-resolution-using-gans-keras-aca310f33112) - -## Problem Statement: - Enhancing low resolution images by applying deep network with adversarial network (Generative Adversarial Networks) - to produce high resolutions images. - -## Architecture: - -![Basic Architecture](./Architecture_images/architecture.jpg) - -## Generator and Discriminator Network: - -![Network](./Architecture_images/network.jpg) - -## Network Details: - * 16 Residual blocks used. - * PixelShuffler x2: This is feature map upscaling. 2 sub-pixel CNN are used in Generator. - * PRelu(Parameterized Relu): We are using PRelu in place of Relu or LeakyRelu. It introduces learn-able parameter - that makes it possible to adaptively learn the negative part coefficient. - * k3n64s1 this means kernel 3, channels 64 and strides 1. - * Loss Function: We are using Perceptual loss. It comprises of Content(Reconstruction) loss and Adversarial loss. - -## How it Works: - * We process the HR(High Resolution) images to get down-sampled LR(Low Resolution) images. Now we have both HR - and LR images for training data set. - * We pass LR images through Generator which up-samples and gives SR(Super Resolution) images. - * We use a discriminator to distinguish the HR images and back-propagate the GAN loss to train the discriminator - and the generator. - * As a result of this, the generator learns to produce more and more realistic images(High Resolution images) as - it trains. - -## Documentation: -You can find more about this implementation in my post : [Single Image Super Resolution Using GANs — Keras](https://medium.com/@birla.deepak26/single-image-super-resolution-using-gans-keras-aca310f33112) - - -## Requirements: - - You will need the following to run the above: - Python 3.5.4 - tensorflow 1.11.0 - keras 2.2.4 - numpy 1.10.4 - matplotlib, skimage, scipy - - For training: Good GPU, I trained my model on NVIDIA Tesla P100 - -## Data set: - - * Used COCO data set 2017. It is around 18GB having images of different dimensions. - * Used 800 images for training(Very less, You can take more (approx. 350 according to original paper) thousand is you can - collect and have very very good GPU). Preprocessing includes cropping images so that we can have same dimension images. - Images with same width and height are preferred. I used images of size 384 for high resolution. - * After above step you have High Resolution images. Now you have to get Low Resolution images which you can get by down - scaling HR images. I used down scale = 4. So Low resolution image of size 96 we will get. Sample code for this. - -## File Structure: - - Network.py : Contains Generator and Discriminator Network - Utils.py : Contains utilities to process images - Utils_model.py : Contains optimizer and content loss code - train.py : Used for training the model - test.py : To test the model - Simplified : This folder contains code without Agrparse etc. If you hate commandline arguements just dive in here. - There are just two files. Modify according to requirement and start training. - -## Usage: - - Note : Image shape and downscale factor you can set in train.py file.Set according to requirement. - - * Training: - Run below command to train model. Set parameters accordingly. - > python train.py --input_dir='./data/' --output_dir='./output/' --model_save_dir='./model/' --batch_size=64 --epochs=3000 --number_of_images=1000 --train_test_ratio=0.8 - - All Parameters have default values. For mode help on parameters run: - > python train.py -h - - * Testing: - test.py file contains code to test. Testing can be done in two ways using option test_type: - 1. Test Model- Here you can test the model by providing HR images. It will process to get resulting LR images and then will generate SR images. - And then will save output file comprising of all LR, SR and HR images. - Run following command to test model: - > python test.py --input_high_res='./data_hr/' --output_dir='./output/' --model_dir='./model/gen_model3000.h5' --number_of_images=25 --test_type='test_model' - For more help run: - > python test.py -h - - 2. Test LR images- This option directly take LR images and give resulting HR images. - Run following command to get HR images from LR images: - > python test.py --input_low_res='./data_lr/' --output_dir='./output/' --model_dir='./model/gen_model3000.h5' --number_of_images=25 --test_type='test_lr_images' - For more help run: - > python test.py -h - - If you hate commandline arguements please reffer Simplified folder. Modify parameters in file like image_shape, input folder - etc. according to your need and start training. - -## Things's Learned: - - * GAN's sometimes are hard to train. Network can be very deep sometimes, but use of residual blocks make it easier. - * Once you get to learn about Perceptual loss things get easier. Same Perceptual loss can be usefull for Image Style Transfer and Photo Realistic Style Transfer. - * This is one of the problem where i struggled to get data. You need to be carefull while choosing data and also preprossing is little bit tough. - * Better to use images with same width and height. - * Use GPU for training else it will take months to train(even you can run out of memory). - -## Output: - -Below are few results- -![Output 1](./output/gan_generated_image_epoch_1110.png) -![Output 2](./output/gan_generated_image_epoch_2580.png) -![Output 2](./output/gan_generated_image_epoch_770.png) - -More results are in output folder - -## Refrences: - - Paper: - Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network: https://arxiv.org/pdf/1609.04802.pdf - Perceptual Losses for Real-Time Style Transfer and Super-Resolution: https://cs.stanford.edu/people/jcjohns/papers/eccv16/JohnsonECCV16.pdf - - Projects doing the same thing: - https://github.com/MathiasGruber/SRGAN-Keras - https://github.com/titu1994/Super-Resolution-using-Generative-Adversarial-Networks - https://github.com/eriklindernoren/Keras-GAN/tree/master/srgan - https://github.com/brade31919/SRGAN-tensorflow - https://github.com/tensorlayer/srgan - - Help on GANS: - https://github.com/eriklindernoren/Keras-GAN (Various GANS implemented in Keras) - https://github.com/JGuillaumin/SuperResGAN-keras - https://oshearesearch.com/index.php/2016/07/01/mnist-generative-adversarial-model-in-keras/ - - VGG loss help: - https://blog.sicara.com/keras-generative-adversarial-networks-image-deblurring-45e3ab6977b5 - - SubpixelConv2D(Deconvolution) help: - Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network: https://arxiv.org/abs/1609.05158 - https://github.com/twairball/keras-subpixel-conv - - Improved Techniques for Training GANs: - https://arxiv.org/abs/1606.03498 - - - - +# SRGAN 介绍 + +## 基本信息 + +**发布者(Publisher):Huawei** + +**应用领域(Application Domain):** 超分辨率 + +**版本(Version):1.1** + +**修改时间(Modified) :2022.5.25**2 + +**大小(Size):8M** + +**框架(Framework):TensorFlow 1.15.0** + +**模型格式(Model Format):.h5** + +**精度(Precision):Mixed** + +**处理器(Processor):昇腾910** + +**应用级别(Categories):Research** + +**描述(Description):基于TF1.15+keras2.2.4的SRGAN复现** + +## 概述 +SRGAN是作者通过GAN这个技术对图片进行超分辨率,先用原图进行下采样获得低分率图像,然后对低分辨率图像使用生成器,把原图当作辨别器来训练,这样收敛后就可以由低分辨率图生成高分辨率图。 + + ++ 参考论文: + + [Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network](https://arxiv.org/pdf/1609.04802.pdf) + + + + + + +## 默认配置 + ++ 训练超参 + + Batch size:16 + + Optimizer:Adam + + Train epoch:500 + + + + +## 支持特性 + +| 特效列表 | 是否支持 | +| -------- | -------- | +| 混合精度 | 是 | + +### 混合精度训练 + +昇腾910 AI处理器提供自动混合精度功能,可以针对全网中float32数据类型的算子,按照内置的优化策略,自动将部分float32的算子降低精度到float16,从而在精度损失很小的情况下提升系统性能并减少内存使用。 + + + + + + +## 模型训练与评测步骤 + +### For train + ++ 在modelArts上训练,入口地址为 modelarts_entry_acc.py 文件。 + ++ 通过入口地址文件 执行 训练脚本 train_full_1p.sh。 + + ++ 在脚本文件中,执行train代码。 训练使用预训练,训练数据集为resize后的COCO数据,其中6400条作为训练集,1600条作为验证集。 + + ```sh + python3.7 ./train.py \ + --input_dir=${data_path} \ + --output_dir=${output_path} \ + --batch_size=16 \ + --epochs=500 \ + --number_of_images=800 \ + --train_test_ratio=0.8 + ``` + ++ 训练的每个阶段的代码保存在 obs 的 model 文件夹中,格式为h5。 + + + + + + +## GPU与NPU 精度与性能比对 +- 精度结果比对 + +|精度指标项|gan_loss1|GPU实测|NPU实测| +|---|---|---|---| +|loss| |0.006|0006| + +|精度指标项|gan_loss2|GPU实测|NPU实测| +|---|---|---|---| +|loss| |0.004|0003| + +|精度指标项|gan_loss3|GPU实测|NPU实测| +|---|---|---|---| +|loss| |1.9|1.9| + +|精度指标项|discriminator_loss|GPU实测|NPU实测| +|---|---|---|---| +|loss| |0.3|0.3| + +-性能 + +一样快 + -- Gitee From 9b719e51afec5c641caf234b56e6ceb352fa7785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Wed, 25 May 2022 07:11:58 +0000 Subject: [PATCH 05/11] update TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh. --- .../cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh index 95eb1ba9c..99285d0cc 100644 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_full_1p.sh @@ -140,8 +140,8 @@ then --input_dir=${data_path} \ --output_dir=${output_path} \ --batch_size=16 \ - --epochs=20 \ - --number_of_images=400 \ + --epochs=500 \ + --number_of_images=8000 \ --train_test_ratio=0.8 else @@ -151,8 +151,8 @@ else --input_dir=${data_path} \ --output_dir=${output_path} \ --batch_size=16 \ - --epochs=20 \ - --number_of_images=400 \ + --epochs=500 \ + --number_of_images=8000 \ --train_test_ratio=0.8 > ${print_log} fi -- Gitee From f3e06ce1f70d9e89761094c65785f749ae683f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Wed, 25 May 2022 07:12:35 +0000 Subject: [PATCH 06/11] update TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md. --- TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md index f133b77d3..3d1ee9990 100644 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md @@ -79,7 +79,7 @@ SRGAN是作者通过GAN这个技术对图片进行超分辨率,先用原图进 --output_dir=${output_path} \ --batch_size=16 \ --epochs=500 \ - --number_of_images=800 \ + --number_of_images=8000 \ --train_test_ratio=0.8 ``` -- Gitee From af4e42b7034b94d3a80bf352d595e33cedf2c94a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Mon, 30 May 2022 07:34:13 +0000 Subject: [PATCH 07/11] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/SRGAN=5FID2087=5Ffor=5FTensorflow/README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cv/SRGAN_ID2087_for_Tensorflow/README.md | 115 ------------------ 1 file changed, 115 deletions(-) delete mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md deleted file mode 100644 index 3d1ee9990..000000000 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/README.md +++ /dev/null @@ -1,115 +0,0 @@ -# SRGAN 介绍 - -## 基本信息 - -**发布者(Publisher):Huawei** - -**应用领域(Application Domain):** 超分辨率 - -**版本(Version):1.1** - -**修改时间(Modified) :2022.5.25**2 - -**大小(Size):8M** - -**框架(Framework):TensorFlow 1.15.0** - -**模型格式(Model Format):.h5** - -**精度(Precision):Mixed** - -**处理器(Processor):昇腾910** - -**应用级别(Categories):Research** - -**描述(Description):基于TF1.15+keras2.2.4的SRGAN复现** - -## 概述 -SRGAN是作者通过GAN这个技术对图片进行超分辨率,先用原图进行下采样获得低分率图像,然后对低分辨率图像使用生成器,把原图当作辨别器来训练,这样收敛后就可以由低分辨率图生成高分辨率图。 - - -+ 参考论文: - - [Photo-Realistic Single Image Super-Resolution Using a Generative Adversarial Network](https://arxiv.org/pdf/1609.04802.pdf) - - - - - - -## 默认配置 - -+ 训练超参 - + Batch size:16 - + Optimizer:Adam - + Train epoch:500 - - - - -## 支持特性 - -| 特效列表 | 是否支持 | -| -------- | -------- | -| 混合精度 | 是 | - -### 混合精度训练 - -昇腾910 AI处理器提供自动混合精度功能,可以针对全网中float32数据类型的算子,按照内置的优化策略,自动将部分float32的算子降低精度到float16,从而在精度损失很小的情况下提升系统性能并减少内存使用。 - - - - - - -## 模型训练与评测步骤 - -### For train - -+ 在modelArts上训练,入口地址为 modelarts_entry_acc.py 文件。 - -+ 通过入口地址文件 执行 训练脚本 train_full_1p.sh。 - - -+ 在脚本文件中,执行train代码。 训练使用预训练,训练数据集为resize后的COCO数据,其中6400条作为训练集,1600条作为验证集。 - - ```sh - python3.7 ./train.py \ - --input_dir=${data_path} \ - --output_dir=${output_path} \ - --batch_size=16 \ - --epochs=500 \ - --number_of_images=8000 \ - --train_test_ratio=0.8 - ``` - -+ 训练的每个阶段的代码保存在 obs 的 model 文件夹中,格式为h5。 - - - - - - -## GPU与NPU 精度与性能比对 -- 精度结果比对 - -|精度指标项|gan_loss1|GPU实测|NPU实测| -|---|---|---|---| -|loss| |0.006|0006| - -|精度指标项|gan_loss2|GPU实测|NPU实测| -|---|---|---|---| -|loss| |0.004|0003| - -|精度指标项|gan_loss3|GPU实测|NPU实测| -|---|---|---|---| -|loss| |1.9|1.9| - -|精度指标项|discriminator_loss|GPU实测|NPU实测| -|---|---|---|---| -|loss| |0.3|0.3| - --性能 - -一样快 - -- Gitee From e50a4cbb1b6acfe147fd09d1a670ef22c72155bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Mon, 30 May 2022 07:35:16 +0000 Subject: [PATCH 08/11] update TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py. --- .../cv/SRGAN_ID2087_for_Tensorflow/Utils.py | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py index 64a40cbdb..573ac7818 100644 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/Utils.py @@ -77,9 +77,9 @@ def load_data_from_dirs(dirs, ext): files.append(image) file_names.append(os.path.join(d,f)) count = count + 1 - if count%1000==0 : - print(count) - print('finish load img') + #if count%1000==0 : + #print(count) + #print('finish load img') return files def load_data(directory, ext): @@ -93,14 +93,14 @@ def load_training_data(directory, ext, number_of_images = 10000, train_test_rati files = load_data_from_dirs(load_path(directory), ext) if len(files) < number_of_images: - print("Number of image files are less then you specified") - print("Please reduce number of images to %d" % len(files)) + #print("Number of image files are less then you specified") + #print("Please reduce number of images to %d" % len(files)) sys.exit() test_array = array(files,dtype=object) if len(test_array[0].shape) < 3: - print("Images are of not same shape") - print("Please provide same shape images") + #print("Images are of not same shape") + #print("Please provide same shape images") sys.exit() x_train = files[:number_of_train_images] @@ -109,9 +109,9 @@ def load_training_data(directory, ext, number_of_images = 10000, train_test_rati x_train_hr = hr_images(x_train) for i in range(number_of_train_images): x_train_hr[i] = normalize(x_train_hr[i]) - if i%1000==0: - print(i) - print("x_train_hr finish normalize") + #if i%1000==0: + #print(i) + #print("x_train_hr finish normalize") x_train_lr = lr_images(x_train, 4) for j in range(number_of_train_images): @@ -121,17 +121,17 @@ def load_training_data(directory, ext, number_of_images = 10000, train_test_rati x_test_hr = hr_images(x_test) for k in range(number_of_images-number_of_train_images): x_test_hr[k] = normalize(x_test_hr[k]) - if k %1000 == 0: - print(k) - print("x_test_hr finish normalize") + #if k %1000 == 0: + #print(k) + #print("x_test_hr finish normalize") x_test_lr = lr_images(x_test, 4) for w in range(number_of_images-number_of_train_images): x_test_lr[w] = normalize(x_test_lr[w]) - if w %1000 == 0: - print(w) - print("x_test_lr finish normalize") + #if w %1000 == 0: + #print(w) + #print("x_test_lr finish normalize") return x_train_lr, x_train_hr, x_test_lr, x_test_hr @@ -142,8 +142,8 @@ def load_test_data_for_model(directory, ext, number_of_images = 100): files = load_data_from_dirs(load_path(directory), ext) if len(files) < number_of_images: - print("Number of image files are less then you specified") - print("Please reduce number of images to %d" % len(files)) + #print("Number of image files are less then you specified") + #print("Please reduce number of images to %d" % len(files)) sys.exit() x_test_hr = hr_images(files) @@ -159,8 +159,8 @@ def load_test_data(directory, ext, number_of_images = 100): files = load_data_from_dirs(load_path(directory), ext) if len(files) < number_of_images: - print("Number of image files are less then you specified") - print("Please reduce number of images to %d" % len(files)) + #print("Number of image files are less then you specified") + #print("Please reduce number of images to %d" % len(files)) sys.exit() x_test_lr = lr_images(files, 4) @@ -173,7 +173,7 @@ def load_test_data(directory, ext, number_of_images = 100): def plot_generated_images(output_dir, epoch, generator, x_test_hr, x_test_lr , dim=(1, 3), figsize=(15, 5)): examples = x_test_hr.shape[0] - print(examples) + #print(examples) value = randint(0, examples) image_batch_hr = denormalize(x_test_hr) image_batch_lr = x_test_lr @@ -238,10 +238,10 @@ def plot_test_generated_images(output_dir, generator, x_test_lr, figsize=(5, 5)) examples = x_test_lr.shape[0] #image_batch_lr = denormalize(x_test_lr) image_batch_lr = x_test_lr - print(image_batch_lr.shape) + #print(image_batch_lr.shape) gen_img = generator.predict(image_batch_lr) generated_image = denormalize(gen_img) - print(generated_image.shape) + #print(generated_image.shape) for index in range(examples): #plt.figure(figsize=figsize) @@ -258,3 +258,5 @@ def plot_test_generated_images(output_dir, generator, x_test_lr, figsize=(5, 5)) + + -- Gitee From ab314600c632de0e08e95f6ee3c5f91609fcad75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Mon, 30 May 2022 07:36:44 +0000 Subject: [PATCH 09/11] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/SRGAN=5FID2087=5Ffor=5FTensorflow/test/trai?= =?UTF-8?q?n=5Fperformance=5F1p.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/train_performance_1p.sh | 203 ------------------ 1 file changed, 203 deletions(-) delete mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh deleted file mode 100644 index 8e39d7a68..000000000 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/test/train_performance_1p.sh +++ /dev/null @@ -1,203 +0,0 @@ -#!/bin/bash - -########################################################## -#########第3行 至 100行,请一定不要、不要、不要修改########## -#########第3行 至 100行,请一定不要、不要、不要修改########## -#########第3行 至 100行,请一定不要、不要、不要修改########## -########################################################## -# shell脚本所在路径 -cur_path=`echo $(cd $(dirname $0);pwd)` - -# 判断当前shell是否是performance -perf_flag=`echo $0 | grep performance | wc -l` - -# 当前执行网络的名称 -Network=`echo $(cd $(dirname $0);pwd) | awk -F"/" '{print $(NF-1)}'` - -export RANK_SIZE=1 -export RANK_ID=0 -export JOB_ID=10087 - -# 路径参数初始化 -data_path="" -output_path="" - -# 帮助信息,不需要修改 -if [[ $1 == --help || $1 == -h ]];then - echo"usage:./train_performance_1P.sh " - echo " " - echo "parameter explain: - --data_path # dataset of training - --output_path # output of training - --train_steps # max_step for training - --train_epochs # max_epoch for training - --batch_size # batch size - -h/--help show help message - " - exit 1 -fi - -# 参数校验,不需要修改 -for para in $* -do - if [[ $para == --data_path* ]];then - data_path=`echo ${para#*=}` - elif [[ $para == --output_path* ]];then - output_path=`echo ${para#*=}` - elif [[ $para == --train_steps* ]];then - train_steps=`echo ${para#*=}` - elif [[ $para == --train_epochs* ]];then - train_epochs=`echo ${para#*=}` - elif [[ $para == --batch_size* ]];then - batch_size=`echo ${para#*=}` - fi -done - -# 校验是否传入data_path,不需要修改 -if [[ $data_path == "" ]];then - echo "[Error] para \"data_path\" must be config" - exit 1 -fi - -# 校验是否传入output_path,不需要修改 -if [[ $output_path == "" ]];then - output_path="./test/output/${ASCEND_DEVICE_ID}" -fi - -# 设置打屏日志文件名,请保留,文件名为${print_log} -print_log="./test/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log" -modelarts_flag=${MODELARTS_MODEL_PATH} -if [ x"${modelarts_flag}" != x ]; -then - echo "running with modelarts..." - print_log_name=`ls /home/ma-user/modelarts/log/ | grep proc-rank` - print_log="/home/ma-user/modelarts/log/${print_log_name}" -fi -echo "### get your log here : ${print_log}" - -CaseName="" -function get_casename() -{ - if [ x"${perf_flag}" = x1 ]; - then - CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'perf' - else - CaseName=${Network}_bs${batch_size}_${RANK_SIZE}'p'_'acc' - fi -} - -# 跳转到code目录 -cd ${cur_path}/../ -rm -rf ./test/output/${ASCEND_DEVICE_ID} -mkdir -p ./test/output/${ASCEND_DEVICE_ID} - -# 训练开始时间记录,不需要修改 -start_time=$(date +%s) -########################################################## -#########第3行 至 100行,请一定不要、不要、不要修改########## -#########第3行 至 100行,请一定不要、不要、不要修改########## -#########第3行 至 100行,请一定不要、不要、不要修改########## -########################################################## - -#========================================================= -#========================================================= -#========训练执行命令,需要根据您的网络进行修改============== -#========================================================= -#========================================================= -# 基础参数,需要模型审视修改 -# 您的训练数据集在${data_path}路径下,请直接使用这个变量获取 -# 您的训练输出目录在${output_path}路径下,请直接使用这个变量获取 -# 您的其他基础参数,可以自定义增加,但是batch_size请保留,并且设置正确的值 -train_epochs=800 -train_steps=100 -batch_size=32 - -if [ x"${modelarts_flag}" != x ]; -then - - - python3.7 ./train.py \ - --input_dir=${data_path} \ - --output_dir=${output_path} \ - --batch_size=32 \ - --epochs=800 \ - --number_of_images=20000 \ - --train_test_ratio=0.8 - -else - - - python3.7 ./train.py \ - --input_dir=${data_path} \ - --output_dir=${output_path} \ - --batch_size=32 \ - --epochs=800 \ - --number_of_images=20000 \ - --train_test_ratio=0.8 - > ${print_log} -fi - -# 性能相关数据计算 -StepTime=`grep "sec/step :" ${print_log} | tail -n 10 | awk '{print $NF}' | awk '{sum+=$1} END {print sum/NR}'` -FPS=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'/'${StepTime}'}'` - -# 精度相关数据计算 -train_accuracy=`grep "Final Accuracy accuracy" ${print_log} | awk '{print $NF}'` -# 提取所有loss打印信息 -grep "loss :" ${print_log} | awk -F ":" '{print $4}' | awk -F "-" '{print $1}' > ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt - - -########################################################### -#########后面的所有内容请不要修改########################### -#########后面的所有内容请不要修改########################### -#########后面的所有内容请不要修改########################### -########################################################### - -# 判断本次执行是否正确使用Ascend NPU -use_npu_flag=`grep "The model has been compiled on the Ascend AI processor" ${print_log} | wc -l` -if [ x"${use_npu_flag}" == x0 ]; -then - echo "------------------ ERROR NOTICE START ------------------" - echo "ERROR, your task haven't used Ascend NPU, please check your npu Migration." - echo "------------------ ERROR NOTICE END------------------" -else - echo "------------------ INFO NOTICE START------------------" - echo "INFO, your task have used Ascend NPU, please check your result." - echo "------------------ INFO NOTICE END------------------" -fi - -# 获取最终的casename,请保留,case文件名为${CaseName} -get_casename - -# 重命名loss文件 -if [ -f ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ]; -then - mv ./test/output/${ASCEND_DEVICE_ID}/my_output_loss.txt ./test/output/${ASCEND_DEVICE_ID}/${CaseName}_loss.txt -fi - -# 训练端到端耗时 -end_time=$(date +%s) -e2e_time=$(( $end_time - $start_time )) - -echo "------------------ Final result ------------------" -# 输出性能FPS/单step耗时/端到端耗时 -echo "Final Performance images/sec : $FPS" -echo "Final Performance sec/step : $StepTime" -echo "E2E Training Duration sec : $e2e_time" - -# 输出训练精度 -echo "Final Train Accuracy : ${train_accuracy}" - -# 最后一个迭代loss值,不需要修改 -ActualLoss=(`awk 'END {print $NF}' $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}_loss.txt`) - -#关键信息打印到${CaseName}.log中,不需要修改 -echo "Network = ${Network}" > $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "RankSize = ${RANK_SIZE}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "BatchSize = ${batch_size}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "DeviceType = `uname -m`" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "CaseName = ${CaseName}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "ActualFPS = ${FPS}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "TrainingTime = ${StepTime}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "ActualLoss = ${ActualLoss}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log -echo "E2ETrainingTime = ${e2e_time}" >> $cur_path/output/$ASCEND_DEVICE_ID/${CaseName}.log \ No newline at end of file -- Gitee From 4795a6456b4c3146858ba586935c5d75db7fdbcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Mon, 30 May 2022 07:40:53 +0000 Subject: [PATCH 10/11] update TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py. --- .../modelarts_entry_acc.py | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py index 8350b7a02..e8fbc67d7 100644 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py +++ b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_acc.py @@ -33,34 +33,33 @@ import sys #print(os.system("pip install keras==2.2.4")) -# 解析输入参数data_url + parser = argparse.ArgumentParser() parser.add_argument("--data_url", type=str, default="/home/ma-user/modelarts/inputs/data_url_0") parser.add_argument("--train_url", type=str, default="/home/ma-user/modelarts/outputs/train_url_0/") config = parser.parse_args() -print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) +#print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) code_dir = sys.path[0] os.chdir(code_dir) -print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) +#print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) -print("[CANN-Modelzoo] before train - list my run files:") +#print("[CANN-Modelzoo] before train - list my run files:") os.system("ls -al /usr/local/Ascend/ascend-toolkit/") -print("[CANN-Modelzoo] before train - list my dataset files:") +#print("[CANN-Modelzoo] before train - list my dataset files:") os.system("ls -al %s" % config.data_url) -print("[CANN-Modelzoo] start run train shell") -# 设置sh文件格式为linux可执行 +#print("[CANN-Modelzoo] start run train shell") + os.system("dos2unix ./test/*") -# 执行train_full_1p.sh或者train_performance_1p.sh,需要用户自己指定 -# full和performance的差异,performance只需要执行很少的step,控制在15分钟以内,主要关注性能FPS + os.system("bash ./test/train_full_1p.sh --data_path=%s --output_path=%s " % (config.data_url, config.train_url)) -print("[CANN-Modelzoo] finish run train shell") +#print("[CANN-Modelzoo] finish run train shell") + -# 将当前执行目录所有文件拷贝到obs的output进行备份 -print("[CANN-Modelzoo] after train - list my output files:") +#print("[CANN-Modelzoo] after train - list my output files:") os.system("cp -r %s %s " % (code_dir, config.train_url)) os.system("ls -al %s" % config.train_url) -- Gitee From 3770714eafcfd4643a8b19ef62113277425d1f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=93=E6=98=9F=E5=AE=87?= <719317604@qq.com> Date: Mon, 30 May 2022 07:46:02 +0000 Subject: [PATCH 11/11] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Te?= =?UTF-8?q?nsorFlow/contrib/cv/SRGAN=5FID2087=5Ffor=5FTensorflow/modelarts?= =?UTF-8?q?=5Fentry=5Fperf.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modelarts_entry_perf.py | 63 ------------------- 1 file changed, 63 deletions(-) delete mode 100644 TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py diff --git a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py b/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py deleted file mode 100644 index 14384e227..000000000 --- a/TensorFlow/contrib/cv/SRGAN_ID2087_for_Tensorflow/modelarts_entry_perf.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -# Copyright 2021 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import os -import argparse -import sys - -# 解析输入参数data_url -parser = argparse.ArgumentParser() -parser.add_argument("--data_url", type=str, default="/home/ma-user/modelarts/inputs/data_url_0") -parser.add_argument("--train_url", type=str, default="/home/ma-user/modelarts/outputs/train_url_0/") -config = parser.parse_args() - -print("[CANN-Modelzoo] code_dir path is [%s]" % (sys.path[0])) -code_dir = sys.path[0] -os.chdir(code_dir) -print("[CANN-Modelzoo] work_dir path is [%s]" % (os.getcwd())) - -print("[CANN-Modelzoo] before train - list my run files:") -os.system("ls -al /usr/local/Ascend/ascend-toolkit/") - -print("[CANN-Modelzoo] before train - list my dataset files:") -os.system("ls -al %s" % config.data_url) - -print("[CANN-Modelzoo] start run train shell") -# 设置sh文件格式为linux可执行 -os.system("dos2unix ./test/*") - -# 执行train_full_1p.sh或者train_performance_1p.sh,需要用户自己指定 -# full和performance的差异,performance只需要执行很少的step,控制在15分钟以内,主要关注性能FPS -os.system("bash ./test/train_performance_1p.sh --data_path=%s --output_path=%s " % (config.data_url, config.train_url)) - -print("[CANN-Modelzoo] finish run train shell") - -# 将当前执行目录所有文件拷贝到obs的output进行备份 -print("[CANN-Modelzoo] after train - list my output files:") -os.system("cp -r %s %s " % (code_dir, config.train_url)) -os.system("ls -al %s" % config.train_url) -- Gitee