-
大小: 1.62MB文件类型: .zip金币: 1下载: 0 次发布日期: 2023-09-30
- 语言: Python
- 标签: cs231n assigment1
资源简介
此资源是cs231n课程assigment1的完整代码,包括knn,svm,softmax,two-layer-nets,feature五个小作业,代码中需要完成的python和ipython均已完成
代码片段和文件信息
import cPickle as pickle
import numpy as np
import os
from scipy.misc import imread
def load_CIFAR_batch(filename):
“““ load single batch of cifar “““
with open(filename ‘rb‘) as f:
datadict = pickle.load(f)
X = datadict[‘data‘]
Y = datadict[‘labels‘]
X = X.reshape(10000 3 32 32).transpose(0231).astype(“float“)
Y = np.array(Y)
return X Y
def load_CIFAR10(ROOT):
“““ load all of cifar “““
xs = []
ys = []
for b in range(16):
f = os.path.join(ROOT ‘data_batch_%d‘ % (b ))
X Y = load_CIFAR_batch(f)
xs.append(X)
ys.append(Y)
Xtr = np.concatenate(xs)
Ytr = np.concatenate(ys)
del X Y
Xte Yte = load_CIFAR_batch(os.path.join(ROOT ‘test_batch‘))
return Xtr Ytr Xte Yte
def load_tiny_imagenet(path dtype=np.float32):
“““
Load TinyImageNet. Each of TinyImageNet-100-A TinyImageNet-100-B and
TinyImageNet-200 have the same directory structure so this can be used
to load any of them.
Inputs:
- path: String giving path to the directory to load.
- dtype: numpy datatype used to load the data.
Returns: A tuple of
- class_names: A list where class_names[i] is a list of strings giving the
WordNet names for class i in the loaded dataset.
- X_train: (N_tr 3 64 64) array of training images
- y_train: (N_tr) array of training labels
- X_val: (N_val 3 64 64) array of validation images
- y_val: (N_val) array of validation labels
- X_test: (N_test 3 64 64) array of testing images.
- y_test: (N_test) array of test labels; if test labels are not available
(such as in student code) then y_test will be None.
“““
# First load wnids
with open(os.path.join(path ‘wnids.txt‘) ‘r‘) as f:
wnids = [x.strip() for x in f]
# Map wnids to integer labels
wnid_to_label = {wnid: i for i wnid in enumerate(wnids)}
# Use words.txt to get names for each class
with open(os.path.join(path ‘words.txt‘) ‘r‘) as f:
wnid_to_words = dict(line.split(‘\t‘) for line in f)
for wnid words in wnid_to_words.iteritems():
wnid_to_words[wnid] = [w.strip() for w in words.split(‘‘)]
class_names = [wnid_to_words[wnid] for wnid in wnids]
# Next load training data.
X_train = []
y_train = []
for i wnid in enumerate(wnids):
if (i + 1) % 20 == 0:
print ‘loading training data for synset %d / %d‘ % (i + 1 len(wnids))
# To figure out the filenames we need to open the boxes file
boxes_file = os.path.join(path ‘train‘ wnid ‘%s_boxes.txt‘ % wnid)
with open(boxes_file ‘r‘) as f:
filenames = [x.split(‘\t‘)[0] for x in f]
num_images = len(filenames)
X_train_block = np.zeros((num_images 3 64 64) dtype=dtype)
y_train_block = wnid_to_label[wnid] * np.ones(num_images dtype=np.int64)
for j img_file in enumerate(filenames):
img_file = os.path.join(path ‘train‘ wnid ‘images‘ img_file)
img = imread(img_file)
if img.ndim == 2:
## grayscale file
img.shape
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6148 2017-03-22 00:20 .DS_Store
目录 0 2017-03-19 11:22 cs231n\
文件 6148 2017-03-22 00:20 cs231n\.DS_Store
文件 0 2016-01-10 03:41 cs231n\__init__.py
文件 105 2017-03-15 00:34 cs231n\__init__.pyc
目录 0 2017-03-20 07:48 cs231n\classifiers\
文件 103 2016-01-10 03:41 cs231n\classifiers\__init__.py
文件 238 2017-03-15 00:34 cs231n\classifiers\__init__.pyc
文件 9178 2017-03-15 02:36 cs231n\classifiers\k_nearest_neighbor.py
文件 5501 2017-03-15 02:36 cs231n\classifiers\k_nearest_neighbor.pyc
文件 6216 2017-03-15 07:18 cs231n\classifiers\linear_classifier.py
文件 4580 2017-03-15 07:18 cs231n\classifiers\linear_classifier.pyc
文件 4838 2017-03-15 07:07 cs231n\classifiers\linear_svm.py
文件 2372 2017-03-15 07:18 cs231n\classifiers\linear_svm.pyc
文件 12084 2017-03-20 07:38 cs231n\classifiers\neural_net.py
文件 7273 2017-03-19 11:06 cs231n\classifiers\neural_net.pyc
文件 3629 2017-03-20 07:48 cs231n\classifiers\softmax.py
文件 2353 2017-03-15 08:18 cs231n\classifiers\softmax.pyc
文件 5550 2016-01-10 03:41 cs231n\data_utils.py
文件 5762 2017-03-15 00:34 cs231n\data_utils.pyc
文件 4807 2016-01-10 03:41 cs231n\features.py
文件 4779 2017-03-19 11:22 cs231n\features.pyc
文件 3904 2017-03-19 10:45 cs231n\gradient_check.py
文件 3824 2017-03-19 10:45 cs231n\gradient_check.pyc
文件 1951 2016-01-10 03:41 cs231n\vis_utils.py
文件 2517 2017-03-19 08:15 cs231n\vis_utils.pyc
文件 354272 2017-03-20 07:58 features.ipynb
文件 412 2016-01-10 03:41 fr
文件 419955 2017-03-15 08:08 knn.ipynb
文件 69063 2017-03-15 08:30 softmax.ipynb
文件 113 2016-01-10 03:41 start_ipython_osx.sh
............此处省略2个文件信息
评论
共有 条评论