特别声明:以下每一段代码:单独放在Google drive 每一个代码框。复制到框内,按照步骤,每步点击按钮即可跑通代码。
科学上网;
下载谷歌浏览器,打开google drive。详见笔者先前写的一个“菜”博客;
挂载谷歌drive:简言之,挂载drive,有利于所有涉及到文件路径时,正常输入,代码运行不出错,【菜博客】有提及较详细的原因。
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={
creds.client_id} -secret={
creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {
vcode} | google-drive-ocamlfuse -headless -id={
creds.client_id} -secret={
creds.client_secret}
!mkdir -p drive
!google-drive-ocamlfuse -o nonempty drive
!rm -rf /content/sample_data
import os
os.chdir('path')
其中的path,可以右点击鼠标挂载后的 Google drive 文件夹(如图)
drive下方很多文件是自己Google 15G云盘的文件。
import os
# #in
path = '/content/ESRGAN_tf2' #在刚搭建搭载的Google drive 创建一个ESRGAN_tf2文件夹
if not os.path.exists(path):
os.makedirs(path)
else:
print('Exist...')
exit(1)
os.chdir(path)
path1 = '/content/ESRGAN_tf2/esrgan-tf2'
if not os.path.exists(path1):
!git clone https://github.com/peteryuX/esrgan-tf2.git
os.chdir(path1)
# os.chdir('/content/temp/esrgan-tf2')
#建立环境
# !pip install -r requirements.txt
#colab默认是tensorflow2.X框架,不用安装环境
import os
path=('/content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K')
if not os.path.exists(path):
os.makedirs(path)
else:
print('Exist...')
exit(1)
os.chdir(path)
# 下载数据
!wget http://data.vision.ee.ethz.ch/cvl/DIV2K/DIV2K_train_HR.zip
!wget http://data.vision.ee.ethz.ch/cvl/DIV2K/DIV2K_train_LR_bicubic_X4.zip
import os
os.chdir('/content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K')
!unzip DIV2K_train_HR.zip
!unzip DIV2K_train_LR_bicubic_X4.zip
#删除下载的压缩包数据
os.chdir('/content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K')
!rm -rf /content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K/DIV2K_train_HR.zip
!rm -rf /content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K/DIV2K_train_LR_bicubic_X4.zip
import os
#os.environ['CUDA_VISIBLE_DEVICES']='0'
os.chdir('/content/ESRGAN_tf2/esrgan-tf2/data')
!python extract_subimages.py
!rm -rf /content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K/DIV2K_train_HR
!rm -rf /content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K/DIV2K_train_LR_bicubic
import os
os.chdir('/content/ESRGAN_tf2/esrgan-tf2/data')
!python convert_train_tfrecord.py
!rm -rf /content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K/DIV2K800_sub
!rm -rf /content/ESRGAN_tf2/esrgan-tf2/data/data/DIV2K/DIV2K800_sub_bicLRx4
import os
os.chdir('/content/ESRGAN_tf2/esrgan-tf2')
!python train_psnr.py
import shutil
shutil.copytree('original_path' , 'destination_path')
import shutil, os
os.chdir('/content/ESRGAN_tf2/esrgan-tf2/checkpoints')
!rm -rf /content/drive/Experiment/ESRGAN_tf2/pre_checkpoint
shutil.copytree('/content/ESRGAN_tf2/esrgan-tf2/checkpoints','/content/drive/Experiment/ESRGAN_tf2/pre_checkpoint') #pre_checkpoint 复制的目的文件,可以不用事先建立,命令可以自动建立。
os.chdir('/content/ESRGAN_tf2/esrgan-tf2/logs')
!rm -rf /content/drive/Experiment/ESRGAN_tf2/tf2_logs
shutil.copytree('/content/ESRGAN_tf2/esrgan-tf2/logs','/content/drive/Experiment/ESRGAN_tf2/tf2_logs')
import os,shutil
os.chdir('/content/drive/Experiment/ESRGAN_tf2/pre_checkpoint')
shutil.copytree('/content/drive/Experiment/ESRGAN_tf2/pre_checkpoint','/content/ESRGAN_tf2/esrgan-tf2/checkpoints')
os.chdir('/content/drive/Experiment/ESRGAN_tf2/tf2_logs')
shutil.copytree('/content/drive/Experiment/ESRGAN_tf2/tf2_logs','/content/ESRGAN_tf2/esrgan-tf2/logs')
os.chdir('/content/ESRGAN_tf2/esrgan-tf2')
!python test.py