-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmp_training_validation_testing (1).py
174 lines (107 loc) · 3.85 KB
/
dmp_training_validation_testing (1).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
"""DMP_TRAINING_VALIDATION_TESTING.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/15t2z-esOVzygVLkJqX9RG01AMsSdeGQq
"""
import numpy as np
from PIL import Image, ImageOps
import matplotlib.pyplot as plt
import h5py
from datetime import datetime
import tensorflow as tf
tf.config.get_visible_devices('GPU')
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as seabornInstance
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
#@title uploader
DIR = "18v2b6y59OqMo56enQa-j5oXLvoESG5jm" #@param {type:"string"}
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# PyDrive reference:
# https://googledrive.github.io/PyDrive/docs/build/html/index.html
from google.colab import auth
auth.authenticate_user()
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')
# Replace the assignment below with your file ID
# to download a different file.
#
# A file ID looks like: 1gLBqEWEBQDYbKCDigHnUXNTkzl-OslSO
import io
from googleapiclient.http import MediaIoBaseDownload
request = drive_service.files().get_media(fileId=DIR)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
# _ is a placeholder for a progress object that we ignore.
# (Our file is small, so we skip reporting progress.)
_, done = downloader.next_chunk()
fileId = drive.CreateFile({'id': DIR }) #DRIVE_FILE_ID is file id example: 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
print(fileId['title'])
fileId.GetContentFile(fileId['title']) # Save Drive file as a local file
!unzip {fileId['title']}
P = []*0
DIR = "/content/data.h5"
files = h5py.File(DIR, 'r')
predcat, predsubcat = "c2", "031_002"
#predimg = np.array([1])
predimg = np.array(files["c2"]["031_002"][100:201, 100:200, :-1].flatten())
labels = []*0
x_rgb, y_label = []*0, []*0
for category in files.keys():
for subcategory in files[category].keys():
if subcategory[:3] not in labels:
labels.append(subcategory[:3])
#dmpimg = np.array([1])
dmpimg = np.array(files[category][subcategory][100:201, 100:200, :-1].flatten())
x_rgb.append(dmpimg)
y_label.append(files[category][subcategory][100:201, 100:200, -1][-1][-1])
x_rgb = np.array(x_rgb, dtype = np.uint8)
x_rgb2 = x_rgb
y_label = np.array(y_label)
from sklearn.utils import shuffle
x_rgb2 = shuffle(x_rgb2, random_state=0)
x_test = x_rgb2[:50]
y_test = []*0
for i in x_test:
for j in range(x_rgb.shape[0]):
if np.array_equal(i, x_rgb[j]) == True:
y_test.append(y_label[j])
break
print(x_test.shape[0], len(y_test))
regressor = LinearRegression()
regressor.fit(x_rgb, y_label)
#To retrieve the intercept:
print(regressor.intercept_)
#For retrieving the slope:
print(regressor.coef_)
y_pred = regressor.predict(x_rgb)
y_pred = np.round(y_pred)
print(np.array(list(zip(y_label, y_pred))))
y_pred = regressor.predict(x_test)
y_pred = np.round(y_pred)
print(np.array(list(zip(y_test, y_pred))))
"""#PREDICTION/CONFUSION MATRIX"""
from sklearn.metrics import confusion_matrix,classification_report
predictions = regressor.predict(x_test)
y_pred = np.round(predictions)
true_classes = y_test
class_labels = y_pred
print(class_labels)
print(confusion_matrix(true_classes, y_pred))
report = classification_report(true_classes, y_pred, zero_division=1)
print(report)