我想使用相对较大的图像数据集(>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提供的那些)。
我可以简单地从 Pycharm 调用 Compute Engine API(在我在其中创建虚拟机之后)来运行我的 python 脚本吗?
或者是否可以在虚拟机中安装 PyCharm 并在其中运行 python 脚本,或者在 docker 容器中编写我的源代码?
总而言之,我怎样才能在 GCP Compute Engine 上简单地运行 python 脚本而不浪费时间在不必要的事情上?