您可以使用 model.save 或对称方法,我将它们存储在数据库缓冲区中,但结果和保存的权重可以在安全驱动器中保持不变。 Google cloud 和 Google Collab 上有很多功能,但是对于数据集存储,Google Drive 就足够了,除了过滤器。
示例:终端距离远,可以节省催款成本。
import io
import os
from os.path import exists
from apiclient import discovery
from httplib2 import Http
from oauth2client import client, file, tools
from googleapiclient.http import MediaIoBaseDownload
import tensorflow as tf
import tensorflow_io as tfio
import matplotlib.pyplot as plt
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
n_folder = 50
encoding = "utf-8"
# define path variables
credentials_file_path = 'F:\temp\Python\credentials\credentials.json'
clientsecret_file_path = 'F:\temp\Python\credentials\client_secret_183167298301-pfhgtdf6k8r4918csmftemgk00ln8l4r.apps.googleusercontent.com.json'
# define API scope
SCOPE = 'https://www.googleapis.com/auth/drive'
# define store
store = file.Storage(credentials_file_path)
credentials = store.get()
# get access token
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(clientsecret_file_path, SCOPE)
credentials = tools.run_flow(flow, store)
# define API service
http = credentials.authorize(Http())
drive = discovery.build('drive', 'v3', http=http)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Fuctions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def download_file( file_id, filename, filetype ):
print( 'downfile: ' + filename + ': ' + filetype )
request = drive.files().get_media( fileId=file_id )
file = io.BytesIO()
downloader = MediaIoBaseDownload( file, request )
done = False
if filetype == "application/vnd.google-apps.folder":
return
try:
while done is False:
status, done = downloader.next_chunk()
print( F'Download {int(status.progress() * 100)}.' )
except HttpError as error:
print(F'An error occurred: {error}')
file = None
tf.io.write_file(
filename, file.getvalue(), name='write_file'
)
return
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Write result to file
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
file = 'F:\datasets\downloads\Actors\train\Pikaploy\01.tif'
image = tf.io.read_file( file )
image = tfio.experimental.image.decode_tiff(image, index=0)
image = tf.image.resize(image, [8,8], method='nearest')
filename='F:\temp\datasets\9.tif'
with open( filename, "wb" ) as f:
b = bytes(str(image), encoding='utf-8')
f.write(b)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Read result to file
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
temp = tf.io.read_file(
filename, name='dataset_9'
)
temp = tf.io.decode_raw(
temp, tf.uint8, little_endian=True, fixed_length=None, name=None
)
temp = tf.constant(temp, shape=(1, 8, 8, 3))
输出:
tf.Tensor(
[[[133 141 126 255]
[ 94 107 90 255]
[106 125 97 255]
[141 140 122 255]
[ 96 114 90 255]
[ 88 106 82 255]
[112 141 93 255]
[116 127 111 255]]
...
[[150 122 111 255]
[180 152 141 255]
[192 160 145 255]
[185 153 138 255]
[168 148 139 255]
[189 158 138 255]
[166 136 110 255]
[ 68 83 64 255]]], shape=(8, 8, 4), dtype=uint8)