Skip to content

Instantly share code, notes, and snippets.

@tebba-von-mathenstein
Created July 11, 2023 18:06
Show Gist options
  • Save tebba-von-mathenstein/57e9dfd251e62a6ff998f0cebeb734c1 to your computer and use it in GitHub Desktop.
Save tebba-von-mathenstein/57e9dfd251e62a6ff998f0cebeb734c1 to your computer and use it in GitHub Desktop.
LabReport_07_14_2023
import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Edge detection
img = cv.imread('amber-kipp-75715CVEJhI-unsplash.jpg')
assert img is not None, "file could not be read, check with os.path.exists()"
edges = cv.Canny(img,80,150)
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()
# Training a neural net on a 2 dimentional function of your choice
num_samples = 100000
rng = np.random.RandomState(12)
x = (200 * rng.rand(num_samples)) - 100
y = (x**3) + (np.sin(x) * 100000) - (3000*x) - x**2
column_x = x[:, np.newaxis]
plt.scatter(x, y, s=1)
model = Sequential([
Dense(units=500, activation='relu', input_shape=(1,)),
Dense(units=250, activation='relu'),
Dense(units=125, activation='relu'),
Dense(units=125, activation='relu'),
Dense(units=125, activation='relu'),
Dense(units=1, activation='linear')
])
model.compile(optimizer="adam", loss='mse')
model.fit(column_x, y, batch_size=200, epochs=300, verbose=True)
xfit = np.linspace(-100, 100, 2000)
yfit = model.predict(xfit[:, np.newaxis])
plt.scatter(x, y, s=1)
plt.plot(xfit, yfit, color='red');
# Fit a linear regression model on the same function
m2 = LinearRegression()
m2.fit(column_x, y)
yfit = m2.predict(xfit[:, np.newaxis])
plt.scatter(x, y, s=1)
plt.plot(xfit, yfit, color='red');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment