print('hello world!')#万能测试
hello world!
测试GPU运行速度
import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
raise SystemError('GPU device not found')
else:
print(device_name)
/device:GPU:0
!nvidia-smi
Mon Oct 24 08:46:47 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 46C P0 28W / 70W | 312MiB / 15109MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
import torch
print(torch.__version__)
加载TF2
%tensorflow_version 2.9.1 #Colab only includes TensorFlow 2.x; %tensorflow_version has no effect.
import tensorflow
print(tensorflow.__version__)
Colab only includes TensorFlow 2.x; %tensorflow_version has no effect.
2.9.2
添加Google的云硬盘到这个工作环境下
from google.colab import drive
drive.mount('/content/drive/')
Drive already mounted at /content/drive/; to attempt to forcibly remount, call drive.mount("/content/drive/", force_remount=True).
切换工作目录
目录名中最好不要有空格。如果有空格,用到此目录名时需要在空格前加\进行转义
%cd "/content/drive/MyDrive/Learing_COLAB"
%ls #显示当前文件夹下的目录
/content/drive/MyDrive/Learing_COLAB
GitHub源码.zip
import os
# Thisnotepath=os.path.abspath(__file__)#只在代码文件中好用,这里是命令行一样的东西,会报错
Thisnotepath=os.getcwd()
print(Thisnotepath)
/content/drive/MyDrive/Learing_COLAB
# import zipfile
# f = zipfile.ZipFile("/content/drive/MyDrive/Learing_COLAB/GitHub源码.zip",'r') # 压缩文件位置
# for file in f.namelist():
# f.extract(file,"../") # 解压位置
# f.close()
# /content/drive/MyDrive/deep-learning-for-image-processing-master
%cd "/content/drive/MyDrive/deep-learning-for-image-processing-master"
%ls #显示当前文件夹下的目录
/content/drive/MyDrive/deep-learning-for-image-processing-master
[0m[01;34marticle_link[0m/ [01;34mothers_project[0m/ README.md
[01;34mcourse_ppt[0m/ [01;34mpytorch_classification[0m/ summary_problem.md
[01;34mdata_set[0m/ [01;34mpytorch_keypoint[0m/ [01;34mtensorflow_classification[0m/
[01;34mdeploying_service[0m/ [01;34mpytorch_object_detection[0m/
LICENSE [01;34mpytorch_segmentation[0m/
用魔法命令运行.py文件
下载花类的数据集
import os
import zipfile
import matplotlib.pyplot as plt
import numpy as np
import requests
from six.moves import urllib
from tqdm import tqdm
def download_from_url(url, dst):
"""
@param: url to download file
@param: dst place to put the file
"""
file_size = int(urllib.request.urlopen(url).info().get('Content-Length', -1))
if os.path.exists