目录
一、Assignment 1 课程作业要求
具体原版要求见链接http://cs231n.github.io/assignments2019/assignment1/ 给不能FQ的同志搬运下
对cifar10 数据进行图像数据的10分类,分别有以下要求
1: k-Nearest Neighbor classifier (20 points) 求解链接
2: Training a Support Vector Machine (25 points) 求解链接
3: Implement a Softmax classifier (20 points) 求解链接
4: Two-Layer Neural Network (25 points) 求解链接
5: Higher Level Representations: Image Features (10 points)
本文仅完成Two-Layer Neural Network 分类器部分,其他部分请点击上面相关求解链接
二、数据集下载及处理
基本的数据集下载,数据简单批处理,参考https://github.com/Halfish/cs231n/blob/master/assignment1/knn.ipynb中,将下载后的cifar10数据集解压
data_utils.py:
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 29 12:57:18 2020
data_utils.py
@author: acer
"""
import pickle
import numpy as np
import os
def load_cifar_batch(filename):
with open(filename,'rb') as f :
datadict=pickle.load(f,encoding='bytes')
x=datadict[b'data']
y=datadict[b'labels']
x=x.reshape(10000,3,32,32).transpose(0,2,3,1).astype('float')
y=np.array(y)
#以数组形式输出
return x,y
#root 为cifar文件根目录
def load_cifar10(root_path):
xs=[]
ys=[]
# 训练集1-5
for b in range(1,2): #这里只选10000张作为训练集
# 读取训练集图片
f=os.path.join(root_path,'data_batch_%d' % (b,))
#每批读取
x,y=load_cifar_batch(f)
#累加 x y 将5批数据合并
xs.append(x)
ys.append(y)
#数组拼接
Xtrain=np.concatenate(xs)
Ytrain=np.concatenate(ys)
del x ,y
# 读取测试集图片
Xtest,Ytest=load_cifar_batch(os.path.join(root_path,'test_batch'))
return Xtrain,Ytrain,Xtest,Ytest
建立读取数据训练的主文件main.py,调用函数读取并显示cifar10图片
main.py:
import numpy as np
from data_utils import load_cifar10
import matplotlib.pyplot as plt
#提取数据
x_train,y_train,x_test,y_test = load_cifar10('.\cifar-10-python\cifar-10-batches-py')
x_test = x_test[0:2000,:]
y_test = y_test[0:2000]
print('training data shape:',x_train.shape)
print('training labels shape:',y_train.shape)
print('test data shape:',x_test.shape)
print('test labels shape:',y_test.shape)
#载入数据
classes=['plane','car','bird','cat','deer','dog','frog','horse','ship','truck']
num_classes=len(classes)
samples_per_class=3
for y ,cls in enumerate(classes):
idxs=np.flatnonzero(y_train==y)
idxs=np.random.choice(idxs,samples_per_class,replace=False)
for i ,idx in enumerate(idxs):
plt_idx=i*num_classes+y&