資源簡介
Fashion-MNIST.zip(包括raw和processed文件夾)
代碼片段和文件信息
from?__future__?import?print_function
import?torch.utils.data?as?data
from?PIL?import?Image
import?os
import?os.path
import?errno
import?numpy?as?np
import?torch
import?codecs
import?os
import?gzip
root?=?r‘/home/qinhaidong/桌面/書/動手學習深度學習/Dive-into-DL-PyTorch-master/data/Fashion-MNIST‘
raw_folder=‘raw‘
filenames=[‘train-images-idx3-ubyte.gz‘‘train-labels-idx1-ubyte.gz‘‘t10k-images-idx3-ubyte.gz‘‘t10k-labels-idx1-ubyte.gz‘]
for?filename?in?filenames:
????file_path=os.path.join(root?raw_folder?filename)
????with?open(file_path.replace(‘.gz‘?‘‘)?‘wb‘)?as?out_f?\
????????????gzip.GzipFile(file_path)?as?zip_f:
????????out_f.write(zip_f.read())
????os.unlink(file_path)??
def?get_int(b):
????return?int(codecs.encode(b?‘hex‘)?16)
def?read_label_file(path):
????with?open(path?‘rb‘)?as?f:
????????data?=?f.read()
????????assert?get_int(data[:4])?==?2049
????????length?=?get_int(data[4:8])
????????parsed?=?np.frombuffer(data?dtype=np.uint8?offset=8)
????????return?torch.from_numpy(parsed).view(length).long()
def?read_image_file(path):
????with?open(path?‘rb‘)?as?f:
????????data?=?f.read()
????????assert?get_int(data[:4])?==?2051
????????length?=?get_int(data[4:8])
????????num_rows?=?get_int(data[8:12])
????????num_cols?=?get_int(data[12:16])
????????images?=?[]
????????parsed?=?np.frombuffer(data?dtype=np.uint8?offset=16)
????????return?torch.from_numpy(parsed).view(length?num_rows?num_cols)
????
splits?=?(‘byclass‘?‘bymerge‘?‘balanced‘?‘letters‘?‘digits‘?‘mnist‘)
def?_training_file(self?split):
????return?‘training_{}.pt‘.format(split)
def?_test_file(self?split):
????return?‘test_{}.pt‘.format(split)
raw_folder?=?os.path.join(root?raw_folder)
gzip_folder?=?os.path.join(raw_folder?‘gzip‘)
raw_folder=‘raw‘
processed_folder=‘processed‘
training_file?=?‘training.pt‘
test_file?=?‘test.pt‘
training_set?=?(
????read_image_file(os.path.join(root?raw_folder?‘train-images-idx3-ubyte‘))
????read_label_file(os.path.join(root?raw_folder?‘train-labels-idx1-ubyte‘))
)
test_set?=?(
????read_image_file(os.path.join(root?raw_folder?‘t10k-images-idx3-ubyte‘))
????read_label_file(os.path.join(root?raw_folder?‘t10k-labels-idx1-ubyte‘))
)
with?open(os.path.join(root?processed_folder?training_file)?‘wb‘)?as?f:
????torch.save(training_set?f)
with?open(os.path.join(root?processed_folder?test_file)?‘wb‘)?as?f:
????torch.save(test_set?f)
print(‘Done!‘)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-10-10?15:04??Fashion-MNIST\
?????目錄???????????0??2019-10-10?15:13??Fashion-MNIST\processed\
?????文件?????7920442??2019-10-10?15:13??Fashion-MNIST\processed\test.pt
?????文件????47520442??2019-10-10?15:13??Fashion-MNIST\processed\training.pt
?????目錄???????????0??2019-10-10?15:13??Fashion-MNIST\raw\
?????文件????????2431??2019-10-10?15:13??Fashion-MNIST\raw\format.py
?????文件?????7840016??2019-10-10?15:13??Fashion-MNIST\raw\t10k-images-idx3-ubyte
?????文件???????10008??2019-10-10?15:13??Fashion-MNIST\raw\t10k-labels-idx1-ubyte
?????文件????47040016??2019-10-10?15:13??Fashion-MNIST\raw\train-images-idx3-ubyte
?????文件???????60008??2019-10-10?15:13??Fashion-MNIST\raw\train-labels-idx1-ubyte
評論
共有 條評論