Skip to content

Instantly share code, notes, and snippets.

@jamesonthecrow
jamesonthecrow / subreddit_suggester_test_new.py
Created November 11, 2018 03:36
Test the subreddit suggester on the newest 100 posts in each subreddit.
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'])
@jamesonthecrow
jamesonthecrow / inference_subreddit_suggester.swift
Last active November 11, 2018 03:37
Incorporate the subreddit suggester into your iOS app.
// Drag your subredditClassifier.mlmodel to the Xcode project navigator.
// Use the model with the following code.
import NaturalLanguage
let subredditPredictor = try NLModel(mlModel: subredditClassifier().model)
subredditPredictor.predictedLabel(for: "TIL you can use Core ML to suggest subreddits to users.")
@jamesonthecrow
jamesonthecrow / fast_style_transfer_width_multiplier.py
Last active November 27, 2018 23:31
An illustration of fast artistic style transfer with a width multiplier included.
@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)
@jamesonthecrow
jamesonthecrow / fast_style_transfer_small.py
Last active November 27, 2018 23:38
A smaller style transfer network.
@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.
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))
}
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
class ViewController: UIViewController {
var cameraView: UIImageView!
var maskView: UIImageView!
override func viewDidLoad() {
// ...
cameraView = UIImageView(frame: view.bounds)
cameraView.contentMode = .scaleAspectFill
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val onDeviceModel = new ObjectDetectionOnDeviceModel();
val objectPredictor = FritzVision.ObjectDetection.getPredictor(onDeviceModel);
var fritzVisionImage: FritzVisionImage
cameraView.addFrameProcessor { frame ->
if (yuvDataLength == 0) {
//Run this only once
initializeData()
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
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
private val itemMap by lazy {