-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample
More file actions
253 lines (106 loc) · 4.06 KB
/
example
File metadata and controls
253 lines (106 loc) · 4.06 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
Neural network ( A.I for barter prediction )
import tensorflow as tf
# Define the neural network architecture
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(num_features,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1)
])
# Compile the neural network
model.compile(optimizer='adam', loss='mse')
# Train the neural network on the training data
model.fit(train_data, train_labels, epochs=10, batch_size=32)
# Evaluate the neural network on the test data
test_loss = model.evaluate(test_data, test_labels)
NN prediction game simulation ( barter )
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# define input, hidden, and output layer sizes
input_layer_size = 2
hidden_layer_size = 3
output_layer_size = 1
# generate random input data
X = np.random.rand(100, input_layer_size)
# define the model
model = Sequential()
model.add(Dense(hidden_layer_size, input_dim=input_layer_size, activation='relu'))
model.add(Dense(output_layer_size, activation='sigmoid'))
# compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# train the model
y = np.random.randint(2, size=100)
model.fit(X, y, epochs=100, batch_size=10)
# start the game
while True:
input_data = np.random.rand(input_layer_size)
prediction = model.predict(np.array([input_data]))
prediction_category = int(np.round(prediction))
user_guess = input(f"Predict whether {input_data} is in category 0 or 1: ")
if int(user_guess) == prediction_category:
print("Congratulations! Your prediction was correct.")
else:
print("Sorry, your prediction was incorrect.")
A trainable example of AI for synthetic data:
import tensorflow as tf
from tensorflow import keras
# Load the dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Normalize the data
x_train = x_train / 255.0
x_test = x_test / 255.0
# Define the model architecture
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)
])
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)
For video:
import cv2
import numpy as np
import tensorflow as tf
from tensorflow import keras
# Load the model architecture
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)
])
# Load the pre-trained model weights
model.load_weights('model_weights.h5')
# Load the video
cap = cv2.VideoCapture('video.mp4')
# Loop over each frame in the video
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
# Preprocess the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray_resized = cv2.resize(gray, (28, 28))
normalized = gray_resized / 255.0
# Make a prediction
prediction = model.predict(np.array([normalized]))
digit = np.argmax(prediction)
# Display the digit on the frame
cv2.putText(frame, str(digit), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
cv2.imshow('frame', frame)
# Press 'q' to quit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
# Release the resources
cap.release()
cv2.destroyAllWindows()