目录
- 输出路径
- 知识点合集
- 错误合集
- TextDataset类
- 错误1 UnicodeDecodeError: 'ascii' codec can't decode byte 0xbe in position 3: ordinal not in range(128)
- 改错
- 错误2 传参错误
- load_all_captions函数
- IndexError: invalid index of a 0-dim tensor
- CUDA out of memory
- tensorboard 出错
- 没有easydict模型
- yaml
- 'EasyDict' object has no attribute 'iteritems'
- 'EasyDict' object has no attribute 'has_key'
- gpu 输入格式不匹配
- Variable data has to be a tensor,but got list
- inception v3 模型参数下载缓慢
- invalid device
- RuntimeError: module must .... on device cuda:0 (device_ids[0]) but found one of them on device: cpu
输出路径
文件夹下 other locations – computer – 1T
正确写法
错误写法
没有加开头的‘/’
表示路径是相对路径 会放在程序文件夹下
都是之前失败的产物
之所以放这是因为
主函数开头的代码
dir_path = (os.path.abspath(os.path.join(os.path.realpath(__file__), './.'))) #输出main程序所在地址
sys.path.append(dir_path)
开头多加了Computer
'/Computer/1T...'
会报错
知识点合集
list, array, torch的维度(长度)查看方法
链接
查看导入的image形状
dataset = TextDataset('C:\\Users\\10755\\Desktop\\StackGAN-v2-master\\stackgan-v2-test\\birds', split_dir,
base_size=64,
transform=image_transform)
imgs, wrong_imgs, embedding, key=dataset[0]
print(type(imgs)) #list
print(len(imgs)) #3
print('imgs\n',imgs)
for t in imgs:
print(t.shape) #torch.Size([3, 64, 64])、torch.Size([3, 128, 128])、torch.Size([3, 256, 256])
print('t\n',t)
导入的图像是个列表,列表里包含三个tensor,每个tensor的形状分别是[3, 64, 64]、[3, 128, 128]、[3, 256, 256]
real_img = imgs_tcpu[-1][0:num]
[0:num]可能是取前num张图
exp_
统计列表各元素个数
pandas.value_counts(a)
a为所求列表
python字典元素的读取
Dataset Dataload
https://blog.csdn.net/zw__chen/article/details/82806900
不显示warning
运行过程中会出现大量warning
代码中加入
import warnings
warnings.filterwarnings("ignore")
可过滤掉
错误合集
TextDataset类
load_embedding函数
def load_embedding(self, data_dir, embedding_type):
if embedding_type == 'cnn-rnn':
embedding_filename = '/char-CNN-RNN-embeddings.pickle'
elif embedding_type == 'cnn-gru':
embedding_filename = '/char-CNN-GRU-embeddings.pickle'
elif embedding_type == 'skip-thought':
embedding_filename = '/skip-thought-embeddings.pickle'
with open(data_dir + embedding_filename, 'rb') as f:
embeddings = pickle.load(f)
embeddings = np.array(embeddings)
# embedding_shape = [embeddings.shape[-1]]
print('embeddings: ', embeddings.shape)
return embeddings
错误1 UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xbe in position 3: ordinal not in range(128)
embeddings = pickle.load(f)
改错
embeddings = pickle.load(f,encoding=‘bytes’)
或者在头文件加对应包
错误2 传参错误
错误原因
dataset = TextDataset(‘C:\Users\10755\Desktop\StackGAN-v2-master\stackgan-v2-test\birds’, split_dir,
64,
transform=image_transform)
embedding_type 传入出错,调用函数时 属性不对应,需要加属性名传入
dataset = TextDataset(‘C:\Users\10755\Desktop\StackGAN-v2-master\stackgan-v2-test\birds’, split_dir,
base_size= 64,
transform=image_transform)
load_all_captions函数
def load_all_captions(self):
def load_captions(caption_name): # self,
cap_path = caption_name
with open(cap_path, "r") as f:
captions = f.read().decode('utf8').split('\n')
captions = [cap.replace("\ufffd\ufffd", " ")
for cap in captions if len(cap) > 0]
return captions
caption_dict = {}
for key in self.filenames:
caption_name = '%s/text_c10/%s.txt' % (self.data_dir, key)
captions = load_captions(caption_name)
caption_dict[key] = captions
print(caption_dict)
return caption_dict
AttributeError: ‘str’ object has no attribute ‘decode’
captions = f.read().decode(‘utf8’).split(’\n’)
解决:
https://blog.csdn.net/qq_41185868/article/details/82079079
captions = f.read().split(’\n’)
IndexError: invalid index of a 0-dim tensor
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python
解决
CUDA out of memory
减少batch_size
tensorboard 出错
parameter to mergeform() must be instance of same class .expected tensorboard Summary got tensor
错误语句:
summary_D = summary.scalar('G_loss%d' % i, errG.data[0])
self.summary_writer.add_summary(summary_D, count)
找不到解决方法
from torch.utils.tensorboard import SummaryWriter
self.summary_writer.add_scalar('D_loss%d'%idx,errD.item(), count)
tensorboard导入错误
通过torch导入tensorboard:
from torch.utils.tensorboard import SummaryWriter
报错:ImportError: TensorBoard logging requires TensorBoard with Python summary writer installed. This should be available in 1.14 or above.
尝试重新安装tensorboard,检查tensorflow,问题依然存在。
直接从tensorboardx导入:
from tensorboardX import SummaryWriter
运行正常。
没有easydict模型
yaml
pip install pyyaml
多试几次
‘EasyDict’ object has no attribute ‘iteritems’
该错误出现的原因是python版本的变化
python2.X不会报错,而Python3.X会报错,因为Python3.X中去除了iteritems()
解决方法:
将iteritems()替换为items()
‘EasyDict’ object has no attribute ‘has_key’
Python3以后删除了has_key()方法
改成
if my_key not in my_dict:
或
if my_dict.get(my_key) is not None:
gpu 输入格式不匹配
注意输入的gpu格式都为int或str
Variable data has to be a tensor,but got list
代码
def prepare_data(data):
imgs = data
vimgs = []
for i in range(3):
print(imgs[i])
if cfg.CUDA:
vimgs.append(Variable(imgs[i]).cuda())
else:
vimgs.append(Variable(imgs[i]))
return imgs, vimgs
尝试torch.tensor转换成tensor
vimgs.append(Variable(torch.tensor(imgs[i])).cuda())
报错:“ValueError: only one element tensors can be converted to Python scalar
imgs[0][1]形状为[b,3,64,64]
一个列表包含3个tensor,每个tensor形状为64*64
解决
把列表里的三个tensor转成数组
再把整个列表转成tensor
val= torch.tensor([item.cpu().detach().numpy() for item in val]).cuda()
val是要转换的list
expected sequence of length 64 at dim 3 (got 128)
原因是tensor数据的维度不统一,一个有64个元素,另一个128。
根本原因
数据读入错误
imgs = data
改成
imgs = data[0]
inception v3 模型参数下载缓慢
提前下载好 放到对应文件夹下
url = ‘https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth’
state_path = 'inception_v3_google-1a9a5a14.pth' #写全路径
state_dict = torch.load(state_path)
self.model.load_state_dict(state_dict)
invalid device
原因 是 我想停止之前在运行的程序,但是我按了 停止后 系统还没停止好 我就把程序关掉了
导致gpu挂了
nvidia - smi 查看gpu进程
只能重启电脑:
ctrl+alt+t 打开终端
输入
shutdown -r now
后来发现代码可能有问题 代码运行过程中 gpu就挂了
RuntimeError: module must … on device cuda:0 (device_ids[0]) but found one of them on device: cpu
模型的参数和buffer(缓存?)需要在cuda:0上运行,但是只找到设备:CPU。
解决方案: 函数.cuda()
根本原因:
if args.gpu_id != '-1':
cfg.GPU_ID = args.gpu_id
else:
cfg.CUDA = False
def parse_args():
...
parser.add_argument('--gpu', dest='gpu_id', type=str, default='-1')
因为默认为-1,所以没有进行gpu修改,导致没有使用gpu
运行设备紊乱
把-1改成需要用到的设备:‘0,1,2,3’