Skip to content

Instantly share code, notes, and snippets.

View anupamchugh's full-sized avatar
🏠
Working from home

Anupam Chugh anupamchugh

🏠
Working from home
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
let modelConfig = MLModelConfiguration()
modelConfig.computeUnits = .cpuAndGPU
let updateTask = try MLUpdateTask(forModelAt: modelURL, trainingData: batchProvider(), configuration: modelConfig,
progressHandlers: MLUpdateProgressHandlers(forEvents: [.trainingBegin,.epochEnd],
progressHandler: { (contextProgress) in
print(contextProgress.event)
}) { (finalContext) in
if finalContext.task.error?.localizedDescription == nil
@anupamchugh
anupamchugh / TorchModule.h
Created October 24, 2019 15:50
Pytorch Mobile Objective-c header file
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TorchModule : NSObject
- (nullable instancetype)initWithFileAtPath:(NSString*)filePath
NS_SWIFT_NAME(init(fileAtPath:))NS_DESIGNATED_INITIALIZER;
+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
@anupamchugh
anupamchugh / TorchModule.mm
Last active October 24, 2019 15:57
Loading Our Pytorch Mobile Model using Objective-C++ wrapper class and running the inference
#import "TorchModule.h"
#import <LibTorch/LibTorch.h>
@implementation TorchModule {
@protected
torch::jit::script::Module _impl;
}
- (nullable instancetype)initWithFileAtPath:(NSString*)filePath {
self = [super init];
var imageView: UIImageView?
var button: UIButton?
var predictionLabel : UILabel?
func buildUI()
{
imageView = UIImageView(frame: .zero)
imageView?.image = UIImage(named: "placeholder")
imageView?.contentMode = .scaleAspectFit
@objc func showActionSheet(sender: UIButton)
{
let alert = UIAlertController(title: "Select Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
self.launchCamera()
}))
alert.addAction(UIAlertAction(title: "Photos Library", style: .default, handler: { _ in
self.showPhotosLibrary()
}))
import UIKit
extension UIImage {
func resized(to newSize: CGSize, scale: CGFloat = 1) -> UIImage {
let format = UIGraphicsImageRendererFormat.default()
format.scale = scale
let renderer = UIGraphicsImageRenderer(size: newSize, format: format)
let image = renderer.image { _ in
draw(in: CGRect(origin: .zero, size: newSize))
}
import coremltools
coreml_model = coremltools.converters.keras.convert('model.h5', input_names=['image'], output_names=['output'],
image_input_names='image')
coreml_model.author = 'Anupam Chugh'
coreml_model.short_description = 'Cat Dog Classifier converted from a Keras model'
coreml_model.input_description['image'] = 'Takes as input an image'
coreml_model.output_description['output'] = 'Prediction as cat or dog'
struct ContentView: View {
var body: some View {
PolylineMapView()
}
}
struct PolylineMapView: UIViewRepresentable {
func makeCoordinator() -> MapViewCoordinator{
return MapViewCoordinator(self)
func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
if UIScreen.main.traitCollection.userInterfaceStyle == .dark{
return ASAuthorizationAppleIDButton(authorizationButtonType: .signIn, authorizationButtonStyle: .white)
}
else{
return ASAuthorizationAppleIDButton(authorizationButtonType: .signIn, authorizationButtonStyle: .black)
}
}