(See Getting Started with SFrames for setup instructions)
import graphlab
# Limit number of worker processes. This preserves system memory, which prevents hosted notebooks from crashing.
graphlab.set_runtime_config('GRAPHLAB_DEFAULT_NUM_PYLAMBDA_WORKERS', 4)
We will use a popular benchmark dataset in computer vision called CIFAR-10.
(We've reduced the data to just 4 categories = {'cat','bird','automobile','dog'}.)
This dataset is already split into a training set and test set.
image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')
graphlab.canvas.set_target('ipynb')
image_train['image'].show()
We first start by training a classifier on just the raw pixels of the image.
raw_pixel_model = graphlab.logistic_classifier.create(image_train,target='label',
features=['image_array'])
image_test[0:3]['image'].show()
image_test[0:3]['label']
raw_pixel_model.predict(image_test[0:3])
The model makes wrong predictions for all three images.
raw_pixel_model.evaluate(image_test)
The accuracy of this model is poor, getting only about 46% accuracy.
We only have 2005 data points, so it is not possible to train a deep neural network effectively with so little data. Instead, we will use transfer learning: using deep features trained on the full ImageNet dataset, we will train a simple model on this small dataset.
len(image_train)
The two lines below allow us to compute deep features. This computation takes a little while, so we have already computed them and saved the results as a column in the data you loaded.
(Note that if you would like to compute such deep features and have a GPU on your machine, you should use the GPU enabled GraphLab Create, which will be significantly faster for this task.)
函数
extract_features()
:提取特征
# deep_learning_model = graphlab.load_model('http://s3.amazonaws.com/GraphLab-Datasets/deeplearning/imagenet_model_iter45')
# image_train['deep_features'] = deep_learning_model.extract_features(image_train)
As we can see, the column deep_features already contains the pre-computed deep features for this data.
image_train.head()
deep_features_model = graphlab.logistic_classifier.create(image_train,
features=['deep_features'],
target='label')
image_test[0:3]['image'].show()
deep_features_model.predict(image_test[0:3])
The classifier with deep features gets all of these images right!
As we can see, deep features provide us with significantly better accuracy (about 78%)
deep_features_model.evaluate(image_test)