我希望使用相对较大的图像数据集(> 2000 rgb图像)运行一些机器学习算法,如PCA和KNN,以便对这些图像进行分类。
我的源代码如下:
import cv2
import numpy as np
import os
from glob import glob
from sklearn.decomposition import PCA
from sklearn import neighbors
from sklearn import preprocessing
data = []
# Read images from file
for filename in glob('Images/*.jpg'):
img = cv2.imread(filename)
height, width = img.shape[:2]
img = np.array(img)
# Check that all my images are of the same resolution
if height == 529 and width == 940:
# Reshape each image so that it is stored in one line
img = np.concatenate(img, axis=0)
img = np.concatenate(img, axis=0)
data.append(img)
# Normalise data
data = np.array(data)
Norm = preprocessing.Normalizer()
Norm.fit(data)
data = Norm.transform(data)
# PCA model
pca = PCA(0.95)
pca.fit(data)
data = pca.transform(data)
# K-Nearest neighbours
knn = neighbors.NearestNeighbors(n_neighbors=4, algorithm='ball_tree', metric='minkowski').fit(data)
distances, indices = knn.kneighbors(data)
print(indices)
然而,我的笔记本电脑不足以完成这项任务,因为它需要很多小时才能处理700多个rgb图像。所以我需要使用在线平台的计算资源(例如GCP提供的计算资源)。我怎样才能简单地使用GCP的一些资源(更快的CPU,GPU等)来运行上面的源代码?
我可以简单地从Pycharm调用Compute Engine API(在我创建虚拟机之后)来运行我的python脚本吗?
或者唯一可能的解决方案是在虚拟机中安装PyCharm并在其中运行python脚本或执行虚拟机中建议的这些答案(Running a python script on Google Cloud Compute Engine,Run python script on Google Cloud Compute Engine)?
答案 0 :(得分:1)
首先,您似乎需要将图片移动到某个位置,GCP才能访问它们,例如Google云端存储(GCS)。您无法在GCP上运行代码,也无法在其中运行代码。然后,您可以使用Compute Engine来运行您的python代码,可能在docker容器中。您必须扩展您的代码,以便您可以启动流程,访问GCS以获取图像并将结果存储在某处。
我会考虑使用Google Dataproc,如果您认真对待使用云处理大量信息,那么Dataproc是托管服务,可以按比例进行大量工作。它可以从GCS中提取信息,运行代码,将负载分散到一组计算机上,并将结果存储在BigQuery或Cloud SQL等数据库中。