from google.colab import auth

auth.authenticate_user()GOOGLE_CLOUD_PROJECT = "central-hangar-321813"  # @param {type:"string"}
GOOGLE_CLOUD_REGION = "us-central1"  # @param {type:"string"}

MODEL_NAME = "resnet_cifar_latest"  # @param {type:"string"}

TEST_FILENAME = "test-images.txt"  # @param {type:"string"}
TEST_GCS_BUCKET = "gs://batch-prediction-collection"  # @param {type:"string"}
TEST_LOCAL_PATH = "Continuous-Adaptation-for-Machine-Learning-System-to-Data-Changes/notebooks/test-images"  # @param {type:"string"}from os import listdir

test_files = listdir(TEST_LOCAL_PATH)
test_filesf = open(TEST_FILENAME, "w")

for filename in test_files:
    f.write(f"{TEST_GCS_BUCKET}/{filename}\n")

f.close()import google.cloud.aiplatform as aiplatform
from typing import Union, Sequence


def create_batch_prediction_job_dedicated_resources_sample(
    project: str,
    location: str,
    model_resource_name: str,
    job_display_name: str,
    gcs_source: Union[str, Sequence[str]],
    gcs_destination: str,
    instances_format: str = "file-list",
    machine_type: str = "n1-standard-2",
    accelerator_count: int = 1,
    accelerator_type: str = "NVIDIA_TESLA_K80",
    starting_replica_count: int = 1,
    max_replica_count: int = 1,
    sync: bool = True,
):
    aiplatform.init(project=project, location=location)

    my_model = aiplatform.Model(model_resource_name)

    batch_prediction_job = my_model.batch_predict(
        job_display_name=job_display_name,
        instances_format=instances_format,
        gcs_source=gcs_source,
        gcs_destination_prefix=gcs_destination,
        machine_type=machine_type,
        accelerator_count=accelerator_count,
        accelerator_type=accelerator_type,
        starting_replica_count=starting_replica_count,
        max_replica_count=max_replica_count,
        sync=sync,
    )

    batch_prediction_job.wait()

    print(batch_prediction_job.display_name)
    print(batch_prediction_job.resource_name)
    print(batch_prediction_job.state)
    return batch_prediction_jobfrom datetime import datetime

TIMESTAMP = datetime.now().strftime("%Y%m%d%H%M%S")create_batch_prediction_job_dedicated_resources_sample(
    project=GOOGLE_CLOUD_PROJECT,
    location=GOOGLE_CLOUD_REGION,
    model_resource_name="2008244793993330688",
    job_display_name=f"{MODEL_NAME}-{TIMESTAMP}",
    gcs_source=[f"{TEST_GCS_BUCKET}/{TEST_FILENAME}"],
    gcs_destination=f"{TEST_GCS_BUCKET}/results/",
    accelerator_type=None,
    accelerator_count=None,
)import os
import json

RESULTS_DIRECTORY = "results"
RESULTS_DIRECTORY_FULL = f'{TEST_GCS_BUCKET}/{RESULTS_DIRECTORY}'

# Create missing directories
os.makedirs(RESULTS_DIRECTORY, exist_ok=True)

# Get the Cloud Storage paths for each result
!gsutil -m cp -r $RESULTS_DIRECTORY_FULL $RESULTS_DIRECTORY

# Get most recently modified directory
latest_directory = max(
    [
        os.path.join(RESULTS_DIRECTORY, d)
        for d in os.listdir(RESULTS_DIRECTORY)
    ],
    key=os.path.getmtime,
)

# Get downloaded results in directory
results_files = []
for dirpath, subdirs, files in os.walk(latest_directory):
    for file in files:
        if file.startswith("prediction.results"):
            results_files.append(os.path.join(dirpath, file))

# Consolidate all the results into a list
results = []
for results_file in results_files:
    # Download each result
    with open(results_file, "r") as file:
        results.extend([json.loads(line) for line in file.readlines()])resultsnum_correct = 0

for result in results:
    label = os.path.basename(result["instance"]).split("_")[0]
    prediction = result["prediction"]["label"]

    print(f"label({label})/prediction({prediction})")
    if label == prediction:
        num_correct = num_correct + 1

print()
print(f"number of results: {len(results)}")
print(f"number of correct: {num_correct}")
print(f"Accuracy: {num_correct/len(results)}")