• 大小: 9.71MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-09
  • 语言: 其他
  • 标签: zip  

资源简介

CS231N作业例程源码,斯坦福机器学习李飞飞课程课后作业

资源截图

代码片段和文件信息

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 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-12-26 08:57  cs231n-master\
     文件        1137  2017-12-26 08:57  cs231n-master\README.md
     目录           0  2017-12-26 08:57  cs231n-master\assignment1\
     文件          19  2017-12-26 08:57  cs231n-master\assignment1\.gitignore
     目录           0  2017-12-26 08:57  cs231n-master\assignment1\.ipynb_checkpoints\
     文件      361514  2017-12-26 08:57  cs231n-master\assignment1\.ipynb_checkpoints\features-checkpoint.ipynb
     文件      410304  2017-12-26 08:57  cs231n-master\assignment1\.ipynb_checkpoints\knn-checkpoint.ipynb
     文件       67588  2017-12-26 08:57  cs231n-master\assignment1\.ipynb_checkpoints\softmax-checkpoint.ipynb
     文件      352222  2017-12-26 08:57  cs231n-master\assignment1\.ipynb_checkpoints\svm-checkpoint.ipynb
     文件      756677  2017-12-26 08:57  cs231n-master\assignment1\.ipynb_checkpoints\two_layer_net-checkpoint.ipynb
     文件         130  2017-12-26 08:57  cs231n-master\assignment1\README.md
     文件         160  2017-12-26 08:57  cs231n-master\assignment1\collectSubmission.sh
     目录           0  2017-12-26 08:57  cs231n-master\assignment1\cs231n\
     文件           0  2017-12-26 08:57  cs231n-master\assignment1\cs231n\__init__.py
     目录           0  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\
     文件         103  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\__init__.py
     文件        8198  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\k_nearest_neighbor.py
     文件        5611  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\linear_classifier.py
     文件        4564  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\linear_svm.py
     文件       10949  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\neural_net.py
     文件        3187  2017-12-26 08:57  cs231n-master\assignment1\cs231n\classifiers\softmax.py
     文件        5550  2017-12-26 08:57  cs231n-master\assignment1\cs231n\data_utils.py
     目录           0  2017-12-26 08:57  cs231n-master\assignment1\cs231n\datasets\
     文件          88  2017-12-26 08:57  cs231n-master\assignment1\cs231n\datasets\.gitignore
     文件         134  2017-12-26 08:57  cs231n-master\assignment1\cs231n\datasets\get_datasets.sh
     文件        4807  2017-12-26 08:57  cs231n-master\assignment1\cs231n\features.py
     文件        3568  2017-12-26 08:57  cs231n-master\assignment1\cs231n\gradient_check.py
     文件        1951  2017-12-26 08:57  cs231n-master\assignment1\cs231n\vis_utils.py
     文件      361514  2017-12-26 08:57  cs231n-master\assignment1\features.ipynb
     文件         412  2017-12-26 08:57  cs231n-master\assignment1\frameworkpython
     文件      410304  2017-12-26 08:57  cs231n-master\assignment1\knn.ipynb
............此处省略80个文件信息

评论

共有 条评论