计算机视觉知识点-Colaboratory

 

 

你是不是在为下载资源速度慢而发愁?一个1M的论文下载了2个小时,真是火大. 一个30M的python安装包,居然下载了10个小时,一个300M的模型你居然下载了2天. 你是不是想要是有个朋友帮你下载该多好. Colaboratory可以解决这个烦恼.

你是不是看到过一篇论文介绍了一个很厉害的网络,git上还有还有源码,想自己跑一下,但是弄环境弄了3天也没弄好,最后放弃了. 要是有个朋友给你一个弄好的环境改多好.Colaboratory可以解决这个烦恼,在Colaboratory中,keras tensorflow环境已经装好,就差写代码了.

你是不是想弄一个GPU服务器搞一搞*deeplearning*,但是没有预算,Colaboratory可以解决这个烦恼,这里的GPU不要钱.地址:https://colab.research.google.com/notebooks/intro.ipynb

快速下载

对于一些下载比较慢的文件,可以先在这里通过python代码下载到这里的缓存,然后再下载到本地。

下载maskrcnn_resnet50模型例子

import requests
url = 'https://download.pytorch.org/models/maskrcnn_resnet50_fpn_coco-bf2d0c1e.pth'
r = requests.get(url, allow_redirects=True)
open('maskrcnn_resnet50_fpn_coco-bf2d0c1e.pth', 'wb').write(r.content)

模型分析

显示一个vgg的结构,注意下面的代码环境已经弄好,您直接跑就行

from keras.applications import VGG16 
from keras.layers import Input 
baseModel = VGG16(weights="imagenet", include_top=True,   input_tensor=Input(shape=(224, 224, 3))) 
baseModel.summary() 
from keras.utils.vis_utils import plot_model 
plot_model(baseModel, to_file='model_plot.png', show_shapes=True, show_layer_names=True)

使用resnet50在cpu下推理一张图片并测试耗时

from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

import time
start = time.time()
preds = model.predict(x)
end = time.time()
print("[INFO] classification took {} seconds".format(end - start))
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])

模型可视化

from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model
model = Sequential()
model.add(Dense(2, input_dim=1, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
print(model.summary())
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)

 

使用GPU,3分钟训练一个LeNet网络

step1  菜单-->文件-->新建笔记本.  新建一个笔记本.

step2 菜单-->修改-->笔记本设置. 选择GPU.

step3 把下面3个代码片段贴上去.

代码段1 定义Lenet模块

# import the necessary packages
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras import backend as K
class LeNet:
	@staticmethod
	def build(numChannels, imgRows, imgCols, numClasses,
		activation="relu", weightsPath=None):
		# initialize the model
		model = Sequential()
		inputShape = (imgRows, imgCols, numChannels)
		# if we are using "channels first", update the input shape
		if K.image_data_format() == "channels_first":
			inputShape = (numChannels, imgRows, imgCols)
		# define the first set of CONV => ACTIVATION => POOL layers
		model.add(Conv2D(20, 5, padding="same",
			input_shape=inputShape))
		model.add(Activation(activation))
		model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
		# define the second set of CONV => ACTIVATION => POOL layers
		model.add(Conv2D(50, 5, padding="same"))
		model.add(Activation(activation))
		model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
		# define the first FC => ACTIVATION layers
		model.add(Flatten())
		model.add(Dense(500))
		model.add(Activation(activation))
		# define the second FC layer
		model.add(Dense(numClasses))
		# lastly, define the soft-max classifier
		model.add(Activation("softmax"))
		# if a weights path is supplied (inicating that the model was
		# pre-trained), then load the weights
		if weightsPath is not None:
			model.load_weights(weightsPath)
		# return the constructed network architecture
		return model

代码段2 训练模型

from sklearn.model_selection import train_test_split
from keras.datasets import mnist
from keras.optimizers import SGD
from keras.utils import np_utils
from keras import backend as K
import numpy as np
import argparse
import cv2
((trainData, trainLabels), (testData, testLabels)) = mnist.load_data()
# if we are using "channels first" ordering, then reshape the
# design matrix such that the matrix is:
# num_samples x depth x rows x columns
if K.image_data_format() == "channels_first":
	trainData = trainData.reshape((trainData.shape[0], 1, 28, 28))
	testData = testData.reshape((testData.shape[0], 1, 28, 28))
# otherwise, we are using "channels last" ordering, so the design
# matrix shape should be: num_samples x rows x columns x depth
else:
	trainData = trainData.reshape((trainData.shape[0], 28, 28, 1))
	testData = testData.reshape((testData.shape[0], 28, 28, 1))

# scale data to the range of [0, 1]
trainData = trainData.astype("float32") / 255.0
testData = testData.astype("float32") / 255.0

# transform the training and testing labels into vectors in the
# range [0, classes] -- this generates a vector for each label,
# where the index of the label is set to `1` and all other entries
# to `0`; in the case of MNIST, there are 10 class labels
trainLabels = np_utils.to_categorical(trainLabels, 10)
testLabels = np_utils.to_categorical(testLabels, 10)

# initialize the optimizer and model
print("[INFO] compiling model...")
opt = SGD(lr=0.01)
model = LeNet.build(numChannels=1, imgRows=28, imgCols=28,
	numClasses=10)
model.compile(loss="categorical_crossentropy", optimizer=opt,
	metrics=["accuracy"])
# only train and evaluate the model if we *are not* loading a
# pre-existing model

print("[INFO] training...")
model.fit(trainData, trainLabels, batch_size=128, epochs=20,
  verbose=1)
# show the accuracy on the testing set
print("[INFO] evaluating...")
(loss, accuracy) = model.evaluate(testData, testLabels,
  batch_size=128, verbose=1)
print("[INFO] accuracy: {:.2f}%".format(accuracy * 100))
# check to see if the model should be saved to file

print("[INFO] dumping weights to file...")
model.save_weights("lent_weight.hdf5", overwrite=True)

代码段3 测试模型

# randomly select a few testing digits
for i in np.random.choice(np.arange(0, len(testLabels)), size=(10,)):
	# classify the digit
	probs = model.predict(testData[np.newaxis, i])
	prediction = probs.argmax(axis=1)
	# extract the image from the testData if using "channels_first"
	# ordering
	if K.image_data_format() == "channels_first":
		image = (testData[i][0] * 255).astype("uint8")
	# otherwise we are using "channels_last" ordering
	else:
		image = (testData[i] * 255).astype("uint8")
	# merge the channels into one image
	image = cv2.merge([image] * 3)
	# resize the image from a 28 x 28 image to a 96 x 96 image so we
	# can better see it
	image = cv2.resize(image, (96, 96), interpolation=cv2.INTER_LINEAR)
	# show the image and prediction
	cv2.putText(image, str(prediction[0]), (5, 20),
				cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)
	print("[INFO] Predicted: {}, Actual: {}".format(prediction[0],
		np.argmax(testLabels[i])))
	cv2.imwrite("img_{}.jpg".format(i), image)

step4 运行第一个代码段, 然后运行第二个代码段,然后运行第三个代码段. 训练的信息是这样的

Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 2s 0us/step
[INFO] compiling model...
[INFO] training...
Epoch 1/20
60000/60000 [==============================] - 11s 185us/step - loss: 0.9350 - accuracy: 0.7550
Epoch 2/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.2576 - accuracy: 0.9238
Epoch 3/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.1834 - accuracy: 0.9458
Epoch 4/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.1415 - accuracy: 0.9591
Epoch 5/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.1166 - accuracy: 0.9657
Epoch 6/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.0996 - accuracy: 0.9706
Epoch 7/20
60000/60000 [==============================] - 5s 87us/step - loss: 0.0876 - accuracy: 0.9740
Epoch 8/20
60000/60000 [==============================] - 5s 90us/step - loss: 0.0785 - accuracy: 0.9767
Epoch 9/20
60000/60000 [==============================] - 5s 87us/step - loss: 0.0712 - accuracy: 0.9783
Epoch 10/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0658 - accuracy: 0.9801
Epoch 11/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0605 - accuracy: 0.9820
Epoch 12/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.0570 - accuracy: 0.9829
Epoch 13/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0530 - accuracy: 0.9842
Epoch 14/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0508 - accuracy: 0.9849
Epoch 15/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.0482 - accuracy: 0.9855
Epoch 16/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.0455 - accuracy: 0.9865
Epoch 17/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0435 - accuracy: 0.9870
Epoch 18/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0418 - accuracy: 0.9872
Epoch 19/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.0401 - accuracy: 0.9881
Epoch 20/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0384 - accuracy: 0.9886
[INFO] evaluating...
10000/10000 [==============================] - 0s 46us/step
[INFO] accuracy: 98.63%
[INFO] dumping weights to file...

得到的测试图片是这样的

 

一些其它的有用的知识

查看自己的笔记 :文件-》打开文件夹

使用GPU加速: 修改-》笔记本设置-》硬件加速器

解压代码例子

from zipfile import ZipFile
# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
# Extract all the contents of zip file in current directory
zipObj.extractall()

感慨:

刚才看了下,我的上一篇博客还是在2015年,5年过去了,物是人非啊.2015年的时候我换了一个城市工作,压力很大,几年来,有顺心的事,也有烦心的事,不过总体还不错,祝福一下自己.

最后的话:

我是一个工作10年的程序员,工作中经常会遇到需要查一些关键技术,但是很多技术名词的介绍都写的很繁琐,为什么没有一个简单的/5分钟能说清楚的博客呢. 我打算有空就写写这种风格的指南文档.CSDN上搜蓝色的杯子, 没事多留言,指出我写的不对的地方,写的排版风格之类的问题,让我们一起爱智求真吧.wisdomfriend@126.com是我的邮箱,也可以给我邮箱留言.
 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值