我在Windows 64位上使用python 3.6.0b2。
以下是一个使用图像训练keras
模型的python代码。
train_imgs, train_vessels =utils.get_imgs(train_dir, augmentation=True, dataset=dataset,img_size=img_size )
train_vessels=np.expand_dims(train_vessels, axis=3)
n_all_imgs=train_imgs.shape[0]
n_train_imgs=int((1-val_ratio)*n_all_imgs)
train_indices=np.random.choice(n_all_imgs,n_train_imgs,replace=False)
train_batch_fetcher=utils.TrainBatchFetcher(train_imgs[train_indices,...], train_vessels[train_indices,...], batch_size)
val_imgs, val_vessels=train_imgs[np.delete(range(n_all_imgs),train_indices),...], train_vessels[np.delete(range(n_all_imgs),train_indices),...]
# set test dataset
test_imgs, test_vessels, test_masks=utils.get_imgs(test_dir, augmentation=False, img_size=img_size, dataset=dataset, mask=True)
##############
def get_imgs(target_dir, augmentation, img_size, dataset, mask=False):
if dataset=='DRIVE':
img_files,vessel_files, mask_files = DRIVE_files(target_dir)
elif dataset=='STARE':
img_files,vessel_files, mask_files = STARE_files(target_dir)
# load images
fundus_imgs=imagefiles2arrs(img_files)
vessel_imgs=imagefiles2arrs(vessel_files)/255
fundus_imgs=pad_imgs(fundus_imgs, img_size)
vessel_imgs=pad_imgs(vessel_imgs, img_size)
assert(np.min(vessel_imgs)==0 and np.max(vessel_imgs)==1)
if mask:
mask_imgs=imagefiles2arrs(mask_files)/255
mask_imgs=pad_imgs(mask_imgs, img_size)
assert(np.min(mask_imgs)==0 and np.max(mask_imgs)==1)
运行上面的代码时,我得到一个错误。追溯如下。
Traceback (most recent call last):
File "O:\New folder\codes\train - Copy.py", line 89, in <module>
train_imgs, train_vessels =utils.get_imgs(train_dir, augmentation=True, dataset=dataset,img_size=img_size )
File "O:\New folder\codes\utils.py", line 314, in get_imgs
fundus_imgs=imagefiles2arrs(img_files)
UnboundLocalError: local variable 'img_files' referenced before assignment
答案 0 :(得分:0)
img_files 仅在传递if语句的情况下可用,否则它将不绑定到值。
if dataset=='DRIVE':
img_files,vessel_files, mask_files = DRIVE_files(target_dir)
elif dataset=='STARE':
img_files,vessel_files, mask_files = STARE_files(target_dir)
如果数据集不是DRIVE或STARE,则img_files将没有值。
fundus_imgs=imagefiles2arrs(img_files) # img_files has to have some value before you can use it.