Get into the habit of writing together! This is the 8th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event
Google Colab, full name Colaboratory, is a free cloud platform provided by Google, which can use pytorch, keras, tensorflow and other frameworks for deep learning. Its GPU is Tesla T4 GPU, which has strong computing power. For users who are just getting started with machine learning or deep learning, this platform is the best choice.
@[TOC] (Google colab runs github code AttnGAN detailed steps deep learning)
Open colab and connect to the cloud virtual machine
1. Enter Google Drive and find colaboratory in More 2. The Colab interface is shown in the figure below, which is equivalent to Jupyter. It is displayed in the form of a document and can run and share code. 3. Click connect to connect to the virtual machine provided by Google Colab. After waiting , it will display that the connection is successful . 4. Change the virtual machine settings, use GPU, and prepare to start white prostitution. Click on the upper left corner to modify - notebook settings - hardware acceleration to GPU
Configure experimental resources (code+dataset)
1. Run the following code to mount Google Drive to the virtual machine
from google.colab import drive
drive.mount('/content/drive')
复制代码
You can see that it is loaded into the drive folder, which is Google Drive (you can realize the connection between cloud disk files and virtual machines)
2. Clone the github project to the sample_data folder of the virtual machine (usually in this folder, or elsewhere) First find the HHTPS address of the github code, such as AttnGAN: then enter the clone command in colab:
!git clone https://github.com/davidstap/AttnGAN.git
复制代码
You can see the experimental code of the colab virtual machine cloned to github. 3. Import resources according to the prompts of the project
①将谷歌云盘的鸟类预处理的元数据保存到自己的云盘(添加快捷方式就是) 然后打开colab可以看到,birds.zip已经在我们的云盘中,我们将其挪到AttnGAN的指定位置中(data/) 挪入后发现,他是zip文件,要进行解压 故我们通过cd命令,进入data文件夹当中。
cd AttnGAN/
cd data/
复制代码
然后进行解压
!unzip birds.zip
复制代码
同样,我们发现在解压后的birds中还有一个压缩文件叫做text.zip,同样进行解压,解压成功后可以删掉zip文件,因为colab限制了磁盘存储空间,要省着用,解压成功后的文件目录是这样:
②、用与①相当的方法,添加鸟类图像数据集的快捷方式到云盘,并将它们挪到data/birds/并且解压。
此处为大家介绍一小部分可能会用到的命令:
返回上一级:cd .. 查看当前目录:pwd 查看当前目录里的所有文件和文件夹:ls 解压zip格式的压缩文件(text.zip为目标文件):unzip text.zip 解压tar格式的压缩文件(CUB_200_2011.tgz为目标文件):!tar -xf CUB_200_2011.tgz
实验运行
1、预训练 DAMSM 模型,回到code目录下,输入以下代码并运行:
!python pretrain_DAMSM.py --cfg cfg/DAMSM/bird.yml --gpu 0
复制代码
bug1:会出现
IndexError: invalid index of a 0-dim tensor. Use
tensor.item()
in Python ortensor.item<T>()
in C++ to convert a 0-dim tensor to a number
是因为s_cur_loss0 = s_total_loss0[0]应该改成s_cur_loss0 = s_total_loss0.item(),把他们四兄弟都改成.item()
bug2:会出现
File "/content/sample_data/AttnGAN/code/miscc/utils.py", line 104, in build_super_images
drawCaption(text_convas, captions, ixtoword, vis_size)
复制代码
File "/content/sample_data/AttnGAN/code/miscc/utils.py", line 35, in drawCaption fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50) OSError: cannot open resource
这是因为没有FreeMono字体原因,找到utils.py的50行,改为Humor-Sans.ttf或者LiberationMono-Bold.ttf
后面还有少许bug,这里不再赘述
2、训练 AttnGAN 模型
!python main.py --cfg cfg/bird_attn2.yml --gpu 2
复制代码
会提示bug:
RuntimeError: CUDA error: invalid device ordinal
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1.
这个是因为,该虚拟机只有一个gpu可用,将--gpu 2改为--gpu 0
3、采样 采样部分文本测试 可以在这里更改自己需要的文本,比如:this bird is red and white with big swings and short beak
采样部分文件名测试
python main.py --cfg cfg/eval_bird.yml --gpu 0
复制代码
然后可以看到,程序进入正常运行阶段,大功告成。
运行结果
The input text is: this bird is red and white with big swings and short beak Attention input The images generated by the three- stage generator are: The experiment is successful.
Notes on using colab
0. The use of colab's GPU is completely free, use it casually, use it indiscriminately, use it hard, and come on, let's use Google's wool together. 1. Add ! in front of the execution command line. 2. Self-contained library and library installation Colab comes with Pytorch, Tensorflow, Matplotlib, Numpy, Pandas and other deep learning basic libraries. If you need other dependencies, such as Keras, you can create a new code block and use pip to install it. 3. If you use it continuously for more than 12 hours, you will be limited. The model file will be saved in the middle of use. 4. If the virtual machine does not detect your commands and actions for a long time , it will automatically cut off the connection, and everything has to start all over again. 5. Don’t be tempted to charge money to upgrade permissions. There are also many excellent cloud services in China.