This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
def get_listing(subreddit, sort='top', after=None, limit=25): | |
# Set the user agent on the request so we don't get rate limited | |
headers = {'User-agent': 'Subreddit Title Bot'} | |
url_fmt = 'https://www.reddit.com/r/{subreddit}/{sort}.json' | |
url = url_fmt.format(subreddit=subreddit, sort=sort) | |
params = {'after': after, 'limit': limit, 't': 'year'} | |
response = requests.get(url, params=params, headers=headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Test the model on some titles I wrote with specific subreddits in mind. | |
sample_titles = [ | |
'Saw this good boy at the park today.', | |
'Latest NIPS submission from OpenAI', | |
'TIL you can use Core ML to suggest subreddits to users', | |
'I made a tutorial using CreateML AMA', | |
'Westworld and Game of Thrones coming to netflix', | |
'We park in driveways, but drive on parkways', | |
'From the top of Mt. Fuji, Japan', | |
"What's the first thing you do every morning?", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import coremltools | |
# Scrape the 100 newest posts from each subreddit | |
max_posts = 100 | |
posts = [] | |
for subreddit in subreddits: | |
posts.extend(get_n_posts(max_posts, subreddit, sort='new')) | |
# Apply the same preprocessing to the data | |
new_df = pandas.DataFrame(posts, columns=['subreddit', 'title']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CreateML | |
import Foundation | |
// Load our data into an MLDataTable object. | |
let dataFilename = "PATH/TO/data.json" | |
let data = try MLDataTable(contentsOf: URL(fileURLWithPath: dataFilename)) | |
print(data.description) | |
/* | |
Columns: | |
label string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@classmethod | |
def build( | |
cls, | |
image_size, | |
alpha=1.0, | |
input_tensor=None, | |
checkpoint_file=None): | |
"""Build a Transfer Network Model using keras' functional API. | |
Args: | |
image_size - the size of the input and output image (H, W) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@classmethod | |
def build( | |
cls, | |
image_size, | |
alpha=1.0, | |
input_tensor=None, | |
checkpoint_file=None): | |
"""Build a Small Transfer Network Model using keras' functional API. | |
This architecture removes some blocks of layers and reduces the size | |
of convolutions to save on computation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private var machineIdentifier: String { | |
// Returns a machine identifier string. E.g. iPhone10,3 or iPhone7,1 | |
// A full list of machine identifiers can be found here: | |
// https://gist.github.com/adamawolf/3048717 | |
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { return simulatorModelIdentifier } | |
var systemInfo = utsname() | |
uname(&systemInfo) | |
return withUnsafeMutablePointer(to: &systemInfo.machine) { | |
ptr in String(cString: UnsafeRawPointer(ptr).assumingMemoryBound(to: CChar.self)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Fritz | |
// Fetch all of the models matching the tag | |
// we chose based on the machine identifier | |
let tagManager = ModelTagManager(tags: [tag]) | |
// Loop through all of the models returned and download each model. | |
// In this case, we should only have a single model for each tag. | |
var allModels: [FritzMLModel] = [] | |
tagManager.fetchManagedModelsForTags { managedModels, error in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ViewController: UIViewController { | |
var cameraView: UIImageView! | |
var maskView: UIImageView! | |
override func viewDidLoad() { | |
// ... | |
cameraView = UIImageView(frame: view.bounds) | |
cameraView.contentMode = .scaleAspectFill |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private lateinit var renderScript: RenderScript | |
private lateinit var yuvToRGB: ScriptIntrinsicYuvToRGB | |
private var yuvDataLength: Int = 0 | |
private lateinit var allocationIn: Allocation | |
private lateinit var allocationOut: Allocation | |
private lateinit var bitmapOut: Bitmap | |
//Rest of the code |